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
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?
Re: Need help with Assignment
This is my code for first exercise,
Quote:
Code java:
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?
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
Re: Need help with Assignment
Edited. I still haven't any idea for exercise
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:
Quote:
node i of out contains the sum of the data in in's nodes up to and including node i of list in.