Closed Thread Icon

Topic awaiting preservation: Insertion and sorting with Doubly Linked Lists (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12477" title="Pages that link to Topic awaiting preservation: Insertion and sorting with Doubly Linked Lists (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Insertion and sorting with Doubly Linked Lists <span class="small">(Page 1 of 1)</span>\

 
synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 10-17-2002 23:43

ok, here's what I'm trying to do (unsuccessfully):
? If the list is empty, or the new node is smaller than the header node, insert the new node at the front of the list.
? Go through the list until the current node is bigger than the new node and stop. Now insert the new node in front of the bigger node.

What I'm trying to do is make a doubly linked list where adding nodes automatically sorts it from smallest to largest (by adding the new node in the appropiate slot).

Here's my code - which doesn't totally work:

code:
public void insert (Object o) {
Node newNode = new Node(o), current;

if (isEmpty() &#0124; &#0124; ((Customer)header.data).id.compareTo(((Customer)o).id) > 0) {
newNode.next = header;
header = newNode;
return;
}

current = header;
while (current.next != null &#0124; &#0124; ((Customer)current.data).id.compareTo(((Customer)o).id) < 0) {
if (current.next == null) {
current.next = newNode;
newNode.prev = current;
return;
}
current = current.next;
}

newNode.prev = current.prev;
newNode.next = current;
current.prev = newNode;
}



Please help, I'm wigging out...

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-18-2002 00:21

What language is this? If it's C++, then you probably want that second return to be a break, and I'm wondering about the lack of -> operators.

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 10-18-2002 00:28

It's Java.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-18-2002 00:34

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 10-18-2002 00:38

I solved my problem...

code:
public void insert (Object o) {
Node newNode = new Node(o), current = header;

if (isEmpty()) {
header = newNode;
return;
}

if (((Customer)header.data).id.compareTo(((Customer)o).id) > 0) {
newNode.next = header;
header.prev = newNode;
header = newNode;
return;
}

while (current.next != null &#0124; &#0124; ((Customer)current.data).id.compareTo(((Customer)o).id) < 0) {
if (current.next == null) {
current.next = newNode;
newNode.prev = current;
return;
}
current = current.next;
}

newNode.prev = current.prev;
current.prev.next = newNode;
newNode.next = current;
current.prev = newNode;
}



I HAD to use doubly linked lists, InI - this is for an assignment.

Thanks for looking though guys, I'll be sure to post more problems here if they arise.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-18-2002 00:43

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 10-18-2002 01:03

Actually we had to write our own list, node and iterator classes - we couldn't use the one included in the standard java library.
Also, this is a doubly linked list - not singly.

[This message has been edited by synax (edited 10-18-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-18-2002 09:54

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 10-18-2002 12:08

ini, why are you always linking to the 1.2 api?
i mean, the actual version is 1.4

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-18-2002 12:20

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

« BackwardsOnwards »

Show Forum Drop Down Menu