一、Definition A double-ended list(双端链表) is similar to a simple linked list,but it has an additional feature:a reference to the last
link as well as to thefirst. It can discribe as the following picture:二、Implement Now, wecan implement this data structure with java language. We only add a last linkwhich refereces to the last link at the begaining of the simple linked list,and change a littile based
on the simple linked list(simple linked list).Theimplement code is like this:[java]view
plaincopyprint?1. public class DoubleEndedList 2. { 3. private Link first; 4. private Link last; 5. 6. public DoubleEndedList() 7. { 8. first=null; 9. last=null; 10. } 11.12. public boolean isEmpty() 13. { 14. return (first==null); 15. } 16.public class DoubleEndedList{
private Link first;
private Link last;public DoubleEndedList()
{
first=null;
last=null;
}
public boolean isEmpty()
{
return(first==null);
}
[java]view
plaincopyprint?1. 2. public void insertFirst(int iData) 3. { 4. Link newLink=new Link(iData); 5. 6. if(isEmpty()) 7. { 8. last=newLink; 9. } 10.11. newLink.next=first; 12. first=newLink; 13. }14.
public void insertFirst(intiData)
{
LinknewLink=new Link(iData);
if(isEmpty())
{
last=newLink;
}
newLink.next=first;
first=newLink;
}[java]view
plaincopyprint?1. 2. public void insertLast(int iData) 3. { 4. Link newLink=new Link(iData); 5. 6. if(isEmpty()) 7. { 8. first=newLink; 9. } 10. else 11. { 12. last.next=newLink; 13. } 14. last=newLink; 15. } 16.
public void insertLast(intiData)
{
LinknewLink=new Link(iData);
if(isEmpty())
{
first=newLink;
}
else
{
last.next=newLink;
}
last=newLink;
}[java]view
plaincopyprint?1. 2. public int deleteFirst() 3. { 4. int temp=first.iData; 5. //链表中只有一个元素6. if(first.next==null) 7. { 8. last=null; 9. } 10. first=first.next; 11. return temp; 12. } 13.
public int deleteFirst()
{
inttemp=first.iData;
//链表中只有一个元素
if(first.next==null)
{
last=null;
}
first=first.next;
returntemp;
}[java]view
plaincopyprint?1. 2. public void dispaly() 3. { 4. Link current=first; 5. while(current!=null) 6. { 7. current.dispaly(); 8. current=current.next; 9. } 10. } 11.12.13.14.15.16.}public void dispaly()
{
Linkcurrent=first;
while(current!=null)
{
current.dispaly();
current=current.next;
}
}
} Theimplemt class called DoubleEndedList has two important elements frist whichreferences to the first link, and last which references to the last link. Theinsertion and deletion routines are similar to those in a single-endedlist.However, both insertion routines must watch out for the special case whenthe list is empty prior to
the insertion. That is, if isEmpty() is true, theninsertFirst() must set last to the new link, and insertLast() must set first tothe new link. Ifinserting at the beginning with insertFirst(), first is set to point to the newlink, although when inserting at the end with insertLast(), last is set topoint to the new link. Deleting from the start of the list is also
a specialcase if it’s the last item on the list: last must be set to point to null inthis case.(在这里用解释下:在链表头或尾插入节点时,都要判断节点是否为空。如果链表是空的,即没有节点:(1)如果在链表头插入节点时需要将last指向新插入的节点。(2)如果是在链表尾部插入节点,那么需要将first指向新插入的节点。如果链表中只有一个节点,那么我们删除链表头的节点后,需要将last设为空。) insertLast() method can be discribed like the following picture:三、EfficiencyThe efficiency of double-ended link isthe same assimple linked list. We will describe
it in English here:(1)Insertion andDeletetion
Insertion and deletion at the beginning of a linked list are very fastwhich takes O(1) time. But an array is O(N) for this operations. So thelinked list is faster bacause nothing needs to be moved when an item isinserted or deleted.(2)Finding Findingand Accessing a item in a linked list both takes O(n) time, but the time in anarray is O(logn)and O(1) accordingly.
(3) Memory Size Anotherimportant advantage of linked lists over arrays is that a linked list usesexactly as much memory as it needs and can expand to fill all ofavailable memory.The size of an array is
fixed when it's created;this usuallyledas to inefficiency because the array is too large, or to running out of roombecause the array is too small.Vectors, which are expandable arrays, may solvethis problem to some extent, but they usually expand in fixed-sized
increments(such as doubling the size of the array whenever it’s about to overflow). Thissolution is still not as efficient a use of memory as a linked list.ReferenceData Structureand Algrithem in Java,Robert Lafore