public class LinkedList {
private Node top;
public LinkedList() {
top = null;
}
public void add(String data) {
Node temp = new Node(data, top);
top = temp;
}
public void sort() {
LinkedList sortedList = new LinkedList();
Node i;
Node j;
String temp;
String temp2;
String temp3;
for (i = top; i != null; i.getNext()) {
for (j = i.getNext(); j != null; j.getNext()) {
if (i.getData().compareTo(j.getData()) < 0) {
temp = i.getData();
i.setData(temp);
temp2 = j.getData();
j.setData(temp2);
temp3 = temp;
temp = temp2;
temp2 = temp3;
sortedList.add(temp3);
} else if (i.getData().compareTo(j.getData()) > 0) {
temp3 = i.getData();
sortedList.add(temp3);
}
}
}
}
}
Can someone go over my code and tell me why my temp
and temp2
never get assigned and used? And why this isn't working?
When I run my main
I just get the original linked list not the sorted one.
Is my logic correct? I am trying to sort the strings in an ascending order.