public Link delectLast() {// 删除链表最后的元素
Link temp = last;
if (first.next == null)
first = null;
else
last.previous.next = null;
last = last.previous;
return temp;
}
public void showFirst() {// 前向展示
Link current = last;
while (current != null) {
current.showLink();
current = current.previous;
}
}
public void showLast() {// 后向展示
Link current = first;
while (current != null) {
current.showLink();
current = current.next;
}
}
public static void main(String[] args) {
DoubleLink dlink = new DoubleLink();
dlink.insertFirst(1);
dlink.insertFirst(2);
dlink.insertFirst(3);
dlink.showFirst();
dlink.insertLast(4);
dlink.insertLast(5);
dlink.showFirst();
}
}
class Link {
public int idata;// 存放的数据
public Link previous;// 对前一项的引用
public Link next;// 对后一项的引用
public Link(int idata) {
this.idata = idata;
}
public void showLink() {
System.out.print(idata + " ");
}
}