| | |
Algorithm for creating a linked list that is sorted as nodes are inserted
![]() |
•
•
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 23 Days Ago
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 23 Days Ago
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





