JAVA中的链表

2024-12-19 15:43:00
推荐回答(1个)
回答1:

这行中,
first = new Josephu(1, second);
second的location是缺省值0;

public class Josephu {
private int location;
public Josephu nextJose;

public Josephu(){
}
public Josephu(int location,Josephu nextJose){
this.location=location;
this.nextJose=nextJose;
}

public Josephu getNext(){
return nextJose;
}
public void setNext(Josephu nextJose){
this.nextJose=nextJose;
}
public void print(){
System.out.print(location);
}

public static void main(String [] argv){
Josephu fifth = new Josephu(5, null);
Josephu forth = new Josephu(4, fifth);
Josephu third = new Josephu(3, forth);
Josephu second = new Josephu(2, third);
Josephu first = new Josephu(1, second);
first.setNext(second);
second.setNext(third);
third.setNext(forth);
forth.setNext(fifth);
fifth.setNext(first);

first.print();
first.getNext().print();
second.print();
}
}