What's wrong with my sorted linked list algorithm? (Java)

  • Context: Java 
  • Thread starter Thread starter iamsmooth
  • Start date Start date
  • Tags Tags
    Algorithm Java List
Click For Summary

Discussion Overview

The discussion revolves around a Java implementation of a sorted linked list algorithm. Participants explore issues related to infinite loops and incorrect node insertion, focusing on the algorithm's logic and pointer management during node insertion.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their algorithm for inserting nodes into a sorted linked list and notes that it produces an infinite loop when inserting a specific value.
  • Another participant questions the handling of the 'successor' pointer for nodes inserted at the end of the list.
  • A different participant asserts that the first and last nodes should have their predecessor or successor set to null, suggesting that the original poster's code may not be correctly implementing this.
  • Concerns are raised about the absence of null assignments in the code, which could lead to incorrect pointer behavior.
  • One participant suggests that the issue may stem from the first insertion, where a node might end up pointing to itself, leading to the infinite loop.
  • The original poster acknowledges a misunderstanding regarding the use of references versus nodes, which they later resolve, leading to a successful implementation.

Areas of Agreement / Disagreement

Participants express differing views on the specific causes of the infinite loop and the correct handling of node pointers. The discussion includes both agreement on the need for proper pointer management and disagreement on the exact implementation details.

Contextual Notes

Limitations include potential misunderstandings regarding pointer references in Java versus C++, which may affect the clarity of the algorithm's behavior.

iamsmooth
Messages
103
Reaction score
0
So my algorithm compares a node that needs to be inserted with the current linked list set. it goes from first (which contains the lowest integer) and moves forward until it hits null or when the key trying to be inserted is no longer greater than the nodes being compared to.

My input of 23, 17, 3, 28, 8, 12 should yield output of 3, 8, 12, 17, 23, 28

but instead it goes:

3, 8, 17, 23, 23, 23, 23, 23... where 23 becomes an infinite loop.


Code:
      Node traverseInOrder = first; // loops through the linked list (first is preconstructed node)
   
      // 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 can't figure out what I did wrong. I've tried rewriting my algorithm from scratch like 6 times but it's always some different variant of an infinite loop. Can anyone help make some suggestions or even give me a better algorithm? They have to be placed into the list sorted so I can't use any sorting algorithms afterwards.

Thanks!
 
Technology news on Phys.org
What does 'successor' point to in a node you insert at the end of the list?
 
The last node on the list should have successor null, and vise versa with the first node on the list having predecessor of null. I think my code already does that though, doesn't it? I don't see where it's wrong.
 
I don't see any place in your code where you actually set it to null.
 
Code:
// there are 2 cases, as node could be at the end, or the one before the end
      else if (traverseInOrder.successor == null) {
          // this condition is here because if traverse.successor is null, traversal is not incremented
          if (iterator.key.compareTo(traverseInOrder.key) < 0) {
              traverseInOrder.predecessor.successor = iterator;
              iterator.predecessor = traverseInOrder.predecessor;
              iterator.successor = traverseInOrder;
              traverseInOrder.predecessor = iterator;
          } 
          // this covers the case when successor is null, but the key to insert is greater
          else { 
              iterator.predecessor = traverseInOrder;
              traverseInOrder.successor = iterator; 
              //iterator.successor = null // **even if I put this here, nothing happens**
          }
      }

The nodes are constructed with all pointer nodes initialized as null, so I don't think that's what was wrong. I did change them to point to null in the code and it made no difference. Still an infinite loop. Or am I misunderstanding what you're saying?
 
You need to post more code, including initialization and declarations. I suspect that there's something going wrong during the very first insertion, where the node with 23 ends up pointing at itself (lines 14-17 of your fragment).

Code:
     Node traverseInOrder = first; // loops through the linked list (first is preconstructed node)
...
     iterator.successor = traverseInOrder;
...
     first = iterator;

Unfortunately, I know C++ much better than I know Java. In C++, it would've been much clearer (to me, anyways) whether objects being assigned are pointers, references, or values.
 
Last edited:
You were right. I was using a reference to the node's parent instead of the node itself which caused these crazy bugs. After removing it, the program works fine. Thanks!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
8
Views
2K