打印firstnode
地址后,它会打印last node
地址,所以如果我想显示我的地址,我
Linked List
该怎么办?
请给出有效的解决方案
public class LLink {
public Link first, last, tmp;
public void add(int data) {
Link newLink = new Link(data);
if (first == null) {
first = newLink;
System.out.println(first.data);
System.out.println(newLink);
} else {
first.next = newLink;
// first=newLink;
System.out.println(newLink.data);
// newLink = newLink.next;
// newLink.next=newLink;
last = newLink;
}
// newLink.next=first;//it copies the address of old first
// last=newLink;
System.out.println("end");
System.out.println(last);
}
public void delete() {
Link tmp = first;
first = first.next;
System.out.println(first.data);
}
public void display() {
System.out.println("display");
Link e = first;
while (e != null) {
System.out.println(e.data);
System.out.println(e);
System.out.println("first-next" + first.next);
System.out.println(e.next);
e = e.next;
}
}
class Link {
public int data;
public Link next;
public Link(int data) {
this.data = data;
}
}
}