写了一个关于链表的简单程序,出现RT的异常,高手指点一下。先谢了。代码如下:
class Person{
public String name;
public void setname(String b){
name=b;
}
String getname(){
return name;
}
}
class Node {
public Person person;
public Node head;
public Node next;
public boolean isEmpty(){
return head==null;
}//判断链表是否为空
public void printAll(){
Node tmp=head;
if(isEmpty())
System.out.println("这是一个空链表");
while(tmp!=null){
System.out.print(tmp.person.getname()+" ");
tmp=tmp.next;
}
}//遍历链表打印结果
}public class LianBiao {
public static void main(String [] args){
Node nod=new Node();
nod.person.setname("qq");
nod.next=new Node();
nod.head=nod;
Node tem=new Node();
tem=nod.next;
tem.person.setname("qw");
nod.printAll();
}
}
class Person{
public String name;
public void setname(String b){
name=b;
}
String getname(){
return name;
}
}
class Node {
public Person person;
public Node head;
public Node next;
public boolean isEmpty(){
return head==null;
}//判断链表是否为空
public void printAll(){
Node tmp=head;
if(isEmpty())
System.out.println("这是一个空链表");
while(tmp!=null){
System.out.print(tmp.person.getname()+" ");
tmp=tmp.next;
}
}//遍历链表打印结果
}public class LianBiao {
public static void main(String [] args){
Node nod=new Node();
nod.person.setname("qq");
nod.next=new Node();
nod.head=nod;
Node tem=new Node();
tem=nod.next;
tem.person.setname("qw");
nod.printAll();
}
}
建议给Node类加个构造方法
public Node() {
person = new Person();
}