日期:2014-05-20 浏览次数:20748 次
/** * * 双端链表 */ class Link { public long dData; public Link next; public Link(long d) { dData = d; } public void displayLink() { System.out.print(dData + " "); } } class FirstLastList { private Link first; private Link last; public FirstLastList() { first = null; last = null; } public boolean isEmpty() { return first == null; } public void insertFirst(long dd) { Link newlink = new Link(dd); if (isEmpty()) { last = newlink; } newlink.next = first; first = newlink; } //这里看不懂,不知道为什么插入last,然后他插入的值会在first里面,displayList()方法可以打印出first的值,first和last好像都不是同一个Link public void insertlast(long dd) { Link newlink = new Link(dd); if (isEmpty()) { first = newlink; } else { last.next = newlink; } last = newlink; } public long deleteFirst() { long temp = first.dData; if (first.next == null) { last = null; } first = first.next; return temp; } public void displayList() { System.out.print("List(first-->last):"); Link current = first; while(current != null){ current.displayLink(); current = current.next; } System.out.println(""); } } public class Test4 { public static void main(String[] args){ FirstLastList thelist = new FirstLastList(); thelist.insertFirst(22); thelist.insertFirst(44); thelist.insertFirst(66); thelist.insertlast(11); thelist.insertlast(33); thelist.insertlast(55); thelist.displayList(); thelist.deleteFirst(); thelist.deleteFirst(); thelist.displayList(); } }