我们正在学习使用节点的链接列表,我不确定我是否正确执行此操作。我们假设只做一个简单的列表,但是当我去运行程序时,我得到指向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