0

outputI'm doing a project for class where I have to sort a linked list using insertion sort. I am supposed to take user input, convert it into an int array and insert it into a linked list. My problem is for some reason when I go to print the linked list post sort, it only prints the first node. The code worked jsut fine when I was initially testing it(I was manually entering what integers to insert), but now that I'm using arrays it doesn't seem to work. Can anyone help?

(this is only one class from my project but let me know if more information is needed). Edit: I added a picture of what my output lokos like

import java.util.Arrays;
public class SortLL {
 
 static LL top;
 
 static Node head;
 static Node sorted;
 
 //function to insert node at head
 public void toHead(int newData){
 Node newNode = new Node(newData);
 newNode.link = head;
 head = newNode;
 }
 
 public static void insertion(Node ref){ //problem right now is that i'm only passing in one node 
 sorted = null;
 Node current = ref;
 while(current != null){
 Node next = current.link;
 sortInsert(current);
 current = next;
 }
 head = sorted;
 }
 
 static void sortInsert(Node newNode){ //item in this case is val
 if(sorted == null || sorted.item >= newNode.item){
 newNode.link = sorted;
 sorted = newNode;
 } else {
 Node current = sorted;
 while(current.link != null && current.link.item < current.item){
 current = current.link;
 }
 newNode.link = current.link;
 current.link = newNode;
 }
 }
 void printList(Node head) 
 { 
 while (head != null) 
 { 
 System.out.print(head.item + " "); 
 head = head.link; 
 } 
 } 
 public static void sortList(int[] arrA, int[] arrB){
 int[] arr = new int[arrA.length + arrB.length];
 System.arraycopy(arrA, 0, arr, 0, arrA.length);
 System.arraycopy(arrB, 0, arr, arrA.length, arrB.length);
 System.out.println("checking array " + Arrays.toString(arr));
 SortLL sort = new SortLL();
 for(int i=0;i<arr.length;i++){
 sort.toHead(arr[i]);
 }
 
 System.out.println("sortLL.java\n\n");
 sort.printList(sort.head);
 sort.sortInsert(sort.head);
 System.out.println("\nLinkedList After sorting"); 
 sort.printList(sort.head); 
 }
}
asked Apr 10, 2021 at 23:58

1 Answer 1

2

Inside your printList() method, you shift the head variable while iterating over the list. When you move the head variable to the end, you essentially destroy the linked list since you lose your reference to the beginning of it. Java will then automatically treat the unreferenced nodes as garbage.

From what I see, after you first call sort.printList(sort.head), you destroyed your original linked list, so it didn't work when sorting.

When calling printList(), it might help to use a temporary node (Node temp = head) so that you don't lose your original head variable.

answered Apr 11, 2021 at 0:26

1 Comment

So I tried to create a temp as you suggested but it still prints only one variable

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.