Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help with Assignment

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Assignment

    I have an Assignment but now I have no idea for this Assignment. Anyone can give me an idea?
    This is my Assignment


    1.Insert in order Given a doubly linked list of integers sorted from smallest (at the head end) to largest, and a pointer to a single node containing an integer, insert the node in the doubly linked list so that it remains sorted.
    2.Cumulative sum Given a null-terminated doubly linked list, in, create a new null-terminated linked, list out, of the same length, such that node i of out contains the sum of the data in in's nodes up to and including node i of list in. Detect heap exhaustion and report it by setting a boolean variable


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need help with Assignment

    What code do you have that you are having problems with? Please post the code with your questions.
    Do you have the code for the doubly linked list?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Assignment

    This is my code for first exercise,

    public class Node {
          public int item;
          public Node next;
     
          Node() {
              item = 0;
              next = null;
          }
     
          Node(int n) {
              item = n;
              next = null;
          }
     
          Node(int n, Node p) {
              item = n;
              next = p;
          }
    }
     
    public void insertSorted(Node h, int n) {
            Node p = h.next;                  // p points to first real node in list
            Node q = h;                        // q trails p
            while (p != null) {
                if (p.item >= n) {              // found insertion point, between p and q
                    q.next = new Node(n, p);
                    return;
                }
                q = p;
                p = p.next;
            }
            q.next = new Node(n, null);        // Node must be added at end of list
        }
     
        public static void main(String[] args) {
     
            Main m = new Main();
            m.insertSorted(Node, 5);
        }
    }
    But, the second exercise I have no idea, can you help me?
    Last edited by gakon3445; September 14th, 2012 at 01:44 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need help with Assignment

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    The second exercise has several steps listed. Try doing the steps one at a time.
    Which step(s) are you having problems with?

    Start by writing down a list of numbers representing the contents of in
    then write down what would be the contents of out
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Assignment

    Edited. I still haven't any idea for exercise

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need help with Assignment

    Try visualizing the exercise by writing down on a piece of paper a list of numbers in a column representing the contents of the list: in
    then write down in a column next to it what would be the contents of the list: out.

    Use this rule to compute the contents of out:
    node i of out contains the sum of the data in in's nodes up to and including node i of list in.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with my assignment
    By kohii in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 4th, 2012, 03:40 AM
  2. Need help with Tic Tac Toe assignment
    By BAL1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2011, 01:33 PM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 11:01 AM
  4. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM