双端链表
2016-03-03 15:03
267 查看
public class Link { public long lData; public Link next;//reference to next Link public Link(long lData){ this.lData=lData; } public void displayLink1(){ System.out.print("{"+lData+"}"); } }
public 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){//insert at front of list Link newLink = new Link(dd); if(isEmpty()){ last=newLink; } newLink.next=first; first=newLink; } 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.lData; if(first.next==null){ last = null; } first=first.next; return temp; } public void displayList(){ Link current =first; while(current!=null){ current.displayLink1(); current=current.next; } } }
public class FirstLastListApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub 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(); } }