Algorithm for creating a linked list that is sorted as nodes are inserted

Reply

Join Date: Jul 2009
Posts: 35
Reputation: iamsmooth is an unknown quantity at this point 
Solved Threads: 0
iamsmooth iamsmooth is offline Offline
Light Poster

Algorithm for creating a linked list that is sorted as nodes are inserted

 
0
  #1
23 Days Ago
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
  1. put method(newNode) {
  2. Node traverseInOrder = first;
  3. while (newNode > traverseInOrder) {
  4. If (traverseInOrder.successor != null) {
  5. traverseInOrder = traverseInOrder.successor;
  6. }
  7. else
  8. }
  9. newNode.predecessor = traverseInOrder;
  10. newNode.successor = traverseInOrder.successor;
  11. traverseInOrder.successor.predecessor = newNode
  12. traverseInOrder.successor = newNode;
  13. }

That's my basic algorithm, but it's not working. Can anyone give me a description of an algorithm that will work?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
0
  #2
23 Days Ago
I think if you remove the line
  1. traverseInOrder.successor.predecessor = newNode
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.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 35
Reputation: iamsmooth is an unknown quantity at this point 
Solved Threads: 0
iamsmooth iamsmooth is offline Offline
Light Poster
 
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 ><

  1. Node traverseInOrder = first; // loops through the linked list
  2.  
  3. // iterator is the node trying to be inserted
  4. while (iterator.key.compareTo(traverseInOrder.key) > 0) {
  5. if (traverseInOrder.successor != null) {
  6. traverseInOrder = traverseInOrder.successor;
  7. }
  8. else {
  9. break;
  10. }
  11. }
  12. // if node should be inserted in the front
  13. if (traverseInOrder.predecessor == null) {
  14. iterator.successor = traverseInOrder;
  15. traverseInOrder.predecessor = iterator;
  16. iterator.predecessor = null;
  17. first = iterator;
  18. }
  19. // when the node should be inserted at the end
  20. // there are 2 cases, as node could be at the end, or the one before the end
  21. else if (traverseInOrder.successor == null) {
  22. if (iterator.key.compareTo(traverseInOrder.key) < 0) {
  23. traverseInOrder.predecessor.successor = iterator;
  24. iterator.predecessor = traverseInOrder.predecessor;
  25. iterator.successor = traverseInOrder;
  26. traverseInOrder.predecessor = iterator;
  27. }
  28. else {
  29. iterator.predecessor = traverseInOrder;
  30. traverseInOrder.successor = iterator;
  31. }
  32. }
  33. // when the node should be inserted in the middle
  34. else if (traverseInOrder.successor != null && traverseInOrder.predecessor != null) {
  35. iterator.predecessor = traverseInOrder.predecessor;
  36. traverseInOrder.predecessor.successor = iterator;
  37. iterator.successor = traverseInOrder;
  38. traverseInOrder.predecessor = iterator;
  39. }

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!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC