双端链表

1数据结构

 2 特点

保持一个对链表最后一个元素的引用

3 方法摘要

class FirstLastList
   { // -------------------------------------------------------------
   public boolean isEmpty()      ;    // true if no links 
// ------------O[1]-------------------------------------------------
   public void insertFirst(LinkNode dd)  ;// insert at front of list 
// ------------O[1]-------------------------------------------------
   public void insertLast(LinkNode dd)  ; // insert at end of list 
// ------------O[1]-------------------------------------------------
   public long deleteFirst()   ;      // delete first link 
// -------------O[1]------------------------------------------------
   public void displayList();
// -------------------------------------------------------------
   }  // end class FirstLastList

4 代码

4.1 链表节点元素

public class LinkNode extends Object{
    public int iData;//data
    public double dData;//data
    public LinkNode next; //reference to next link
    
    //constructor
    public LinkNode(int id,double dd){
        iData = id;
        dData = dd;
    }
    public void displayLink(){
        //display itself
        System.out.println
        ("iData  :  "+iData+"    dData:"+dData);
    }
}//end class LinkNode

4.2 双向链表

public class FirstLastList {
    public static LinkNode first;
    private static LinkNode last;
    //ref to first link on list
    public void LinkList(){
        first = null;
        last = first;
        // constructor : no items on list yet
    }
    public boolean isEmpty(){
        return first==null;
    }
    /*时间效率 O(1)*/
    public void insertFirst(int i,double d){
        LinkNode newNode = new LinkNode(i,d);
         
            newNode.next = first;
            first = newNode; 
    }
    /*时间效率 O(1)*/
    public void insertLast(int i,double d){
        LinkNode newNode = new LinkNode(i,d);
        if(isEmpty()){
            last= newNode;
            first = last;
        }
         last.next = newNode ; 
         last= newNode;
    }
    public void displayList(){
        LinkNode current = first;
        System.out.println("From first to last   :");
        while(current!=null){
            current.displayLink();
            current = current.next;
        }
    } 
    /*时间效率 O(1)*/
    public LinkNode deleteFirst(){ 
        if (isEmpty()){
            return null;
        }
        LinkNode temp = first;
        first = first.next;
        return temp;
    }
 
    public LinkNode deleteDedecate(LinkNode node){//O[N] : 包含线性查找 
        LinkNode current = first;                                                                   
          LinkNode pre = current;                                                                     
          while (current!=null){//the  loop end condition:traverse to the last node of the link       
              if(current.dData == node.dData && current.iData==node.iData){//find the node                    
                if(current == first) first = first.next;                                                
                  pre.next = current.next;                                                                
                  return current;                                                                         
              }else{//go on to find it                                                                  
                  pre = current;                                                                          
                  current = pre.next;                                                                     
              }                                                                                         
          }                                                                                           
          return null;     
    }
    public LinkNode find(LinkNode node){//O[N] : 包含线性查找 
        LinkNode current = first;
         while (current!=null){
        //     current.displayLink();
             if(current.dData ==node.dData && current.iData == node.iData){
                 return current;
             }
            current = current.next;
         }
        return null;
    }
}

4.3 APP

    public static void main(String[] args) {
                //make a new List
                FirstLastList theList = new FirstLastList();
                //insert nodes
                theList.insertLast(11, 11.11);
                theList.insertLast(22, 22.22);
                theList.insertLast(33, 33.33);
                theList.insertLast(44, 44.44);  
                 
                System.out.println("Before delete  ---");
                theList.displayList();
                theList.deleteFirst();   
                theList.deleteDedecate(new LinkNode(33,33.33));
                System.out.println("After delete  ---");
                theList.displayList();  
                LinkNode node = theList.find(new LinkNode(22,22.22));
                if(node!=null){
                    System.out.println("Find the following one :"+node.iData +"   "+node.dData);
                }
    }
}

 5 双端链表的应用 实现队列Queue

5.1 队列

first in first out(先进先出)

public interface Queue {
//队列尾部插入一个新的元素
    public void insert(LinkNode node);
    //队列头部删除一个元素
    public Object remove();
    //查询队列头部(最前端)的一个元素
    public Object peekFront();
    public void display();
}
/*用双端链表实现队列  */
public class LinkQueue implements Queue{
    private FirstLastList theList;
    public LinkQueue(){
        theList = new FirstLastList();
    }
    public boolean isEmpty(){
        return theList.isEmpty();
    }
 
    public void insert(LinkNode node){
        theList.insertLast(node.iData, node.dData);
    }
    public LinkNode remove(){
        LinkNode node = theList.deleteFirst();
        return node;
    }
    public void display(){
        theList.displayList();
    }
public class LinkQueueApp {
    public static void main(String[] args) {
        LinkQueue que = new LinkQueue();
        que.insert(new LinkNode(10,10.10));
        que.insert(new LinkNode(20,20.20));
        que.insert(new LinkNode(30,30.30));
        que.insert(new LinkNode(40,40.40));
        que.insert(new LinkNode(50,50.50));
        
        que.display();
        System.out.println("Insert has been done  ");
        
        que.remove(); 
        que.display();
        System.out.println("Remove has been done  ");
    }
}

 

6 双端链表特点

6.1 尾部插入新节点 -- > 可以实现队列

7  双端链表缺陷

FirstLastList保存对最后一个节点的引用,不能有助于删除最后一个链结点.因为没有一个引用指向倒数第二个链结点.

===>双向链表可以轻易做到这一点.

posted @ 2013-06-24 04:41  王超_cc  阅读(447)  评论(0编辑  收藏  举报