I am currently studying how a linked list works.
I am having a hard time figuring out why my linked list only has one element.
Method insert() is supposed to accept a parameter and create a linked list sorted in descending order.
/**
* this code is supposed to insert the elements from
* a given input and sort it descendingly.
*/
public void insert(T el) {
SLL<T> linkedList = new SLL();
SLLNode<T> insert = this.head;
if (linkedList.isEmpty()) {
linkedList.addtoHead(el);
linkedList.print();
} else {
linkedList.addtoHead(el);
while (insert.next != null) {
int i = ((Comparable) head.info).compareTo(el);
if (i == 0) {
linkedList.delete(el);
} else if (i > 0) {
linkedList.addtoHead(el);
} else if (i < 0) {
linkedList.addtoHead(el);
}
}
}
}
main()
System.out.println("Enter s1: ");
sc.nextLine();
String s1 = sc.nextLine();
String[] a1 = s1.split(" ");
SLL<Integer> sll1 = new SLL<Integer>();
System.out.println("First Linked List Contents:");
for(int i=0;i<a1.length;i++){
int w = Integer.valueOf(a1[i]);
sll1.insert(w);
}
Here is the input:
Enter s1:
1 2 3
Output is supposed to look like this:
Linked List Contents:
3 2 1