public class MyList {
private int data;
private MyList next;
public MyList(int x) {
data = x;
}
public void add(MyList x) {
x.next = next;
next = x;
}
public void append(MyList x) {
MyList p = this;
while (p.next != null) {
p = p.next;
}
p.next = x;
}
private void show() {
MyList p = this;
while (p != null) {
System.out.println(p.data);
p = p.next;
}
}
public static void main(String[] args) {
MyList head = new MyList(10);
head.append(new MyList(20));
head.append(new MyList(30));
head.append(new MyList(40));
// 向链表中添加一个元素
head.add(new MyList(11));
head.show();
}
}