Problems With Sorting A Linked List In Java

Click For Summary

Discussion Overview

The discussion revolves around the implementation of a sorted linked list in Java, specifically focusing on the add() function within a SortedLinkedList class that uses generics and the Comparable interface to sort objects alphabetically. The scope includes technical problem-solving related to coding and data structures.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • The original poster describes issues with the add() function, particularly with sorting elements between the head and tail of the linked list.
  • Some participants suggest that the problem lies in how the new node is linked to the current node and propose keeping track of a lastNode to correctly insert the new node.
  • A participant confirms that their add() function works after implementing the suggested changes, indicating a resolution to their initial problem.
  • The original poster seeks further resources for understanding data structures after resolving their issue.
  • Another participant shares their approach to learning, emphasizing the importance of reading sample code over specific texts.

Areas of Agreement / Disagreement

The discussion shows a progression from an initial problem to a proposed solution and eventual resolution for the original poster. However, there is no consensus on specific resources for further learning, as participants have differing preferences for learning materials.

Contextual Notes

Participants did not address potential edge cases or performance considerations in the linked list implementation. The discussion focused primarily on the immediate coding issue without exploring broader implications or alternative approaches.

Who May Find This Useful

Individuals interested in Java programming, data structures, and linked list implementations may find this discussion beneficial, particularly those facing similar challenges in coding and algorithm design.

heatengine516
Gold Member
Messages
224
Reaction score
18

Homework Statement



Create a SortedLinkedList class which implements the LinkedListInterface.

Remember to use the Comparable class

Basically, I need to implement a sorted linked list that uses generics and uses compareTo to sort objects. I need it to be able to sort names alphabetically. Can't use priorities or indexes.

Homework Equations


n/a


The Attempt at a Solution



My main problem is getting the add() function to work properly. It can sort the head and tail, but it's all the stuff in the middle that is the problem. It won't properly sort anything in the middle between the head and tail. It must be something wrong with my while loop.

Here is my code for the add() function:
public void add(T data ) {

// new node
if (isEmpty()) {
head = new LinkedListNode<T>();
head.data = data;
head.next = tail;
tail = head;
return;
}

if(head.data.compareTo(data) > 0) {
LinkedListNode<T> node = new LinkedListNode<T>();
node.data = data;
node.next = head;
head = node;

return;
}

if(tail.data.compareTo(data) < 0){
tail.next = new LinkedListNode<T>();
tail = tail.next;
tail.data = data;

return;
}



LinkedListNode<T> current = head;
LinkedListNode<T> newNode = new LinkedListNode<T>();
newNode.data = data;
System.out.println(" init: " +data + " < " + current.data);

while (current.data.compareTo(data) < 0 ) {



System.out.println(data + " < " + current.data);

current = current.next;

System.out.println(data + " < " + current.data);
}



newNode.next = current.next;
current.next = newNode;
return;

}

Here is the input:

public static void main(String[] args)
{
SortedLinkedList list = new SortedLinkedList();

list.add("jesse");
list.add("rick");
list.add("anne");
list.add("brad");
list.getList();
System.out.println();
list.add("jaime");
list.getList();
}
and here is the output after running:
run SortedLinkedList
init: brad < anne
brad < anne
brad < jesse

anne
jesse
brad
rick

init: jaime < anne
jaime < anne
jaime < jesse

anne
jesse
jaime
brad
rick

I have honestly been working on this for a week and I don't understand why it's not working. I've followed in class examples for our priority and linked queues and online tutorials that are similar but nothing works. Any help would be appreciated. Thank you.
 
Physics news on Phys.org
I believe the problem is with:
newNode.next = current.next;
current.next = newNode;

It needs to be:
newNode.next = current;
lastNode.next = newNode;

Then you need to keep track of lastNode, which sill always be the one that was before "current".

So after:
LinkedListNode<T> current = head;
you will have a:
LinkedListNode<T> lastNode = current;

Then before:
current = current.next;
you will have
lastNode = current;

Good luck. Let me know how it goes.
 
Thank you! My add() function is working perfectly now! To think I labored for a week or more trying to find a solution while the fix was so simple. Thank you again.

Do you by any chance have any recommendations for texts/tutorials/resources for further understanding of these data structures?
 
As far as recommended texts, I really don't have any recommendations. Different people learn in different ways. I've been coding for over 4 decades and I pick up new material by reading sample code.

Best of luck!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K