双端链表和传统链表既有相似又有不同,相同点都是通过修改后继指针来添加新结点,但是在此之上,双端链表又加入了一个新的功能,它允许链表的最后一个结点像头结点一样,可以进行直接插入操作。不需要遍历整个链表,效率更高。
举例:
public class link {
public static void main(String[] args) {
// TODO Auto-generated method stub
FLList flList=new FLList();
flList.insertFirst(50);
flList.insertFirst(40);
flList.insertLast(45);
flList.displayList();
flList.deleteFirst();
flList.displayList();
}
}
class Link3{
public long dDate;
public Link3 next;
public Link3(long d){
dDate=d;
}
public void displayLink(){
System.out.println(dDate+" ");
}
}
class FLList{
private Link3 first;
private Link3 last;
public FLList(){
first=null;
last=null;
}
public boolean isEmpty(){
return first==null;
}
public void insertFirst(long dd){ //这里是插入初始结点;
Link3 newLink=new Link3(dd);
if(isEmpty())
last = newLink;
else
newLink.next = first;
first=newLink;
}
public void insertLast(long dd){
Link3 newLink= new Link3(dd);
if(isEmpty())
first=newLink;
else
last.next=newLink;
last=newLink;
}
public long deleteFirst(){
long temp=first.dDate;
if(first.next==null)
last=null;
else
first=first.next;
return temp;
}
public void displayList(){
System.out.println("Link (first->last): ");
Link3 current=first;
while(current!=null){
current.displayLink();
current=current.next;
}
System.out.println(" ");
}
}
输出:
Link (first->last):
4050
45
Link (first->last):
50
45