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

# Thread: Need help with Assignment

1. ## 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. ## 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?

3. ## 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?

4. ## Re: Need help with Assignment

[code=java]
[/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

5. ## Re: Need help with Assignment

Edited. I still haven't any idea for exercise

6. ## 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.