我們正在學習使用節點的鏈接列表,我不確定我是否正確執行此操作。我們假設只做一個簡單的列表,但是當我去運行程序時,我得到指向showList()方法的nullpointerException,但是當我嘗試不使用該方法時,則根本沒有打印出任何東西。請任何幫助,非常感謝。使用節點的列表
public class node {
public int dataitems;
public node next;
node front;
public void initList(){
front = null;
}
public node makeNode(int number){
node newNode;
newNode = new node();
newNode.dataitems = number;
newNode.next = null;
return newNode;
}
public boolean isListEmpty(node front){
boolean balance;
if (front == null){
balance = true;
}
else {
balance = false;
}
return balance;
}
public node findTail(node front) {
node current;
current = front;
while(current.next != null){
//System.out.print(current.dataitems);
current = current.next;
} //System.out.println(current.dataitems);
return current;
}
public void addNode(node front ,int number){
node tail;
if(isListEmpty(front)){
front = makeNode(number);
}
else {
tail = findTail(front);
tail.next = makeNode(number);
}
}
public void printNodes(int len){
int j;
for (j = 0; j < len; j++){
addNode(front, j);
} showList(front);
}
public void showList(node front){
node current;
current = front;
while (current.next != null){
System.out.print(current.dataitems);
current = current.next;
}
System.out.println(current.dataitems);
}
public static void main(String[] args) {
node x = new node();
x.printNodes(50);
}
}
謝謝這工作完美 – TMan