| | |
Algorithm for creating a linked list that is sorted as nodes are inserted
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 35
Reputation:
Solved Threads: 0
So I have these nodes I'm trying to sort as such, but it's not working.
This is psudo code:
Node first = new Node(23); //previously construted node
That's my basic algorithm, but it's not working. Can anyone give me a description of an algorithm that will work?
Thanks!
This is psudo code:
Node first = new Node(23); //previously construted node
Java Syntax (Toggle Plain Text)
put method(newNode) { Node traverseInOrder = first; while (newNode > traverseInOrder) { If (traverseInOrder.successor != null) { traverseInOrder = traverseInOrder.successor; } else } newNode.predecessor = traverseInOrder; newNode.successor = traverseInOrder.successor; traverseInOrder.successor.predecessor = newNode traverseInOrder.successor = newNode; }
That's my basic algorithm, but it's not working. Can anyone give me a description of an algorithm that will work?
Thanks!
0
#2 Nov 7th, 2009
I think if you remove the line
your algorithm will be correct. Remember, node.successor.predecessor should always equal node unless node is the last node in the list. Same with node.predecessor.successor, unless node is the first in the list.
Java Syntax (Toggle Plain Text)
traverseInOrder.successor.predecessor = newNode
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Jul 2009
Posts: 35
Reputation:
Solved Threads: 0
0
#3 Nov 7th, 2009
My code's still not working. I have inputs of:
t.put(23);
t.put(17);
t.put(3);
t.put(28);
t.put(8);
t.put(12);
Output should be 3, 8, 12, 17, 23, 28
But when I iterate it, the output is 3, 8, 17, 23, 23, 23, 23...
23 becomes an infinite loop. Not sure what's wrong with my code ><
I've been stuck on this part of my program for the past 2 days. I thought I knew how to implement a linked list, but apparently not...
Thanks for the help!
t.put(23);
t.put(17);
t.put(3);
t.put(28);
t.put(8);
t.put(12);
Output should be 3, 8, 12, 17, 23, 28
But when I iterate it, the output is 3, 8, 17, 23, 23, 23, 23...
23 becomes an infinite loop. Not sure what's wrong with my code ><
Java Syntax (Toggle Plain Text)
Node traverseInOrder = first; // loops through the linked list // iterator is the node trying to be inserted while (iterator.key.compareTo(traverseInOrder.key) > 0) { if (traverseInOrder.successor != null) { traverseInOrder = traverseInOrder.successor; } else { break; } } // if node should be inserted in the front if (traverseInOrder.predecessor == null) { iterator.successor = traverseInOrder; traverseInOrder.predecessor = iterator; iterator.predecessor = null; first = iterator; } // when the node should be inserted at the end // there are 2 cases, as node could be at the end, or the one before the end else if (traverseInOrder.successor == null) { if (iterator.key.compareTo(traverseInOrder.key) < 0) { traverseInOrder.predecessor.successor = iterator; iterator.predecessor = traverseInOrder.predecessor; iterator.successor = traverseInOrder; traverseInOrder.predecessor = iterator; } else { iterator.predecessor = traverseInOrder; traverseInOrder.successor = iterator; } } // when the node should be inserted in the middle else if (traverseInOrder.successor != null && traverseInOrder.predecessor != null) { iterator.predecessor = traverseInOrder.predecessor; traverseInOrder.predecessor.successor = iterator; iterator.successor = traverseInOrder; traverseInOrder.predecessor = iterator; }
I've been stuck on this part of my program for the past 2 days. I thought I knew how to implement a linked list, but apparently not...
Thanks for the help!
![]() |
Similar Threads
- Simple Linked List with user input (C)
- How to recursively find a value in a linked list (Java)
- sorted linked list (C)
- Graphical Linked List (Java)
- Linked List help (C++)
- Linked List Problem (C++)
- Quicksorting linked list - simple algorithm (C)
- help with linked list (C++)
- Inserting in a sorted linked list(sorted alphabetically) (C++)
Other Threads in the Java Forum
- Previous Thread: how to display private methods in javadoc?
- Next Thread: Boolean class
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor





