I have tried it for the whole afternoon. I even started to inspect my intelligence. It's easy but I really dont know how to do. please help me, thanks very very much!!! implement linked list using java and sort it in alphabetical
public class SinglyLinkedList {
private static final int DEFAULT_MAXIMUM = 10;
private SLLNode[] listArray;
private int first = 0; // first element of the list in the array.
private int firstFree = 0; // first free location in the array.
public static void main(String[] args) {
SinglyLinkedList elementList = new SinglyLinkedList();
elementList.insert("Hydrogen");
elementList.insert("Boron");
elementList.insert("Beryllium");
elementList.insert("Helium");
elementList.insert("Lithium");
elementList.insert("Carbons");
System.out.println(elementList);
}
public SinglyLinkedList() {
listArray = new SLLNode[DEFAULT_MAXIMUM];
}
public SinglyLinkedList(int size) {
listArray = new SLLNode[size];
}
public void insert(String data) {
inserts(data);
}
private void inserts(String data) {
SLLNode tempNode = new SLLNode(data);
listArray[firstFree] = tempNode;
firstFree++;
order();
}
private void order() {
int i = 0;
/*
* for (i = 0; i < firstFree - 1; i++) { if(listArray[i].getNext()==-1) {
* if(listArray[i].comparaTo(listArray[firstFree-1])<0)
* listArray[firstFree-1].setNext(i); else { i++; if (listArray[firstFree -
* 1].comparaTo(listArray[i]) > 0 && listArray[firstFree -
* 1].comparaTo(listArray[listArray[i].getNext()]) < 0) {
* listArray[i].setNext(firstFree - 1); listArray[firstFree -
* 1].setNext(listArray[i].getNext()); } else listArray[firstFree -
* 1].setNext(i); }
*
* } else if (listArray[firstFree - 1].comparaTo(listArray[i]) > 0 &&
* listArray[firstFree - 1].comparaTo(listArray[listArray[i].getNext()]) < 0) {
* listArray[i].setNext(firstFree - 1); listArray[firstFree -
* 1].setNext(listArray[i].getNext()); } else listArray[firstFree -
* 1].setNext(i); }
*/
for (i = 0; i < firstFree - 1; i++) {
if (listArray[i].comparaTo(listArray[first]) < 0)
first = i;
}
SLLNode tempNode = new SLLNode(listArray[first].getData());
tempNode = listArray[first];
/*
* System.out.println("lala"); if(firstFree==1) listArray[first].setNext(0);
* System.out.println("lala");
*/
int j = 0;
if(firstFree==1)
tempNode.setNext(0);
if(firstFree==2) {
listArray[0].setNext(0);
listArray[1].setNext(1);
}
while (tempNode.getNext()!=-1) {
if (listArray[firstFree - 1].comparaTo(tempNode) > 0
&& listArray[firstFree - 1].comparaTo(listArray[tempNode.getNext()]) < 0) {
listArray[firstFree - 1].setNext(tempNode.getNext() - 1);
tempNode.setNext(firstFree-1);
} else {
tempNode = listArray[tempNode.getNext()];
}
}
System.out.println("lala");
}
public String toString() {
StringBuilder tempString = new StringBuilder();
tempString.append("first = " + first + "\n" + "firstFree = " + firstFree + "\n\n");
for (int i = 0; i < firstFree; i++) {
if (listArray[i] != null)
tempString.append(i + ": " + listArray[i].getData() + "\t" + listArray[i].getNext() + "\n");
}
return tempString.toString();
}
/*
* public SLLIterator getIterator() {
*
* }
*/
private static class SLLNode {
private String data;
private int next = -1;
public SLLNode(String data) {
this.data = data;
}
public SLLNode(String data, int next) {
this.data = data;
this.next = next;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return this.data;
}
public void setNext(int next) {
this.next = next;
}
public int getNext() {
return this.next;
}
public String toString() {
return "loading";
}
public int comparaTo(SLLNode tempNode) {
if (data.compareTo(tempNode.getData()) > 0)
return 1;
else if (data.compareTo(tempNode.getData()) < 0)
return -1;
else
return 0;
}
}}
The main problem is the while loop and the nullPointerException I dont know how to determine whether it's the end of the list