双向链表的实现

package DoubleList;

public class DoubleLinkedList {

    private Link first;
    private Link last;
    
    public DoubleLinkedList()
    {
        first = null;
        last = null;
    }
    
    //判断是不是为空
    public boolean isEmpty()
    {
        if(first == null)
        {
            return true;
        }else{
            return false;
        }
    }
    
    //插入元素的实现
    public void insertFirst(long dd)
    {
        //首先创建一个节点
        Link newLink = new Link(dd);
        if(isEmpty())
        {
            last = newLink;
        }else{
            //第一步
            first.previous = newLink;
        }
        //第二步
        newLink.next = first;
        //第三步
        first = newLink;
        
        
        
        
    }
    
    //表尾插入
    public void insertLast(long dd)
    {
        Link newLink = new Link(dd);
        if(isEmpty())
        {
            first = newLink;
        }else{
            
            last.next = newLink;
            newLink.previous = last;
            
        }
        last = newLink;
    }
    //删除头节点
    public Link deleteFirst()
    {
        Link temp = first;
        if(first.next == null)
            last = null;
        else
            first.next.previous = null;
        first = first.previous;
        return temp;
    }
    
    //删除尾节点
    public Link deleteLast()
    {
        Link temp = last;
        if(first.next == null)
        {
            first = null;
        }else
            last.previous.next = null;
            last = last.previous;
            return temp;
    }
    
    //任意节点的插入
    public boolean insertAfter(long key,long dd)
    {
        Link current = first;
        while(current.dData != key)
        {
            current = current.next;
            if(current == null)
            {
                return false;
            }
        }
        
        Link newLink = new Link(dd);
        if(current == last)
        {
            newLink.next = null;
            last = newLink;
        }
        else
        {
            newLink.next = current.next;
            current.next.previous = newLink;
        }
        
        newLink.previous = current;
        current.next = newLink;
        return true;
        
        
        
    }
    
    //删除关键字
    public Link deleteKey(long key)
    {
        Link current = first;
        while(current.dData != key)
        {
            current = current.next;
            if(current == null)
            {
                return null;
            }
        }
        
        if(current == first)
        {
            first = current.next;
        }else{
            current.previous.next = current.next;
        }
        
        if(current == last)
        {
            last = current.previous;
        }else{
            current.next.previous = current.previous;
        }
        
        
        return current;
        
    }
    
    public void displayForward()
    {
        System.out.println("从first------>last");
        Link current = first;
        while(current != null)
        {
            current.displayLink();
            current =current.next;
        }
        System.out.println();
    }
    
    //从后往前打印节点
    public void displayBackward()
    {
        System.out.println("从last-------->first");
        Link current = last;
        while(current != null)
        {
            current.displayLink();
            current = current.previous;
        }
        System.out.println();
    }
    
    
    
    
    
    
}
package DoubleList;

public class Link {

    public long dData;
    public Link next;
    public Link previous;
    public Link(long d)
    {
        dData = d;
    }
    
    public void displayLink()
    {
        System.out.println(dData+"  ");
    }
    
}
package DoubleList;

public class MainApp {

    public static void main(String[] args) {
        DoubleLinkedList theList = new DoubleLinkedList();
        theList.insertFirst(22);
        theList.insertFirst(44);
        theList.insertFirst(66);
        theList.insertLast(11);
        theList.insertLast(33);
        theList.insertLast(55);
        theList.displayForward();
        theList.displayBackward();

    }

}

 

posted on 2014-12-20 22:15  aicpcode  阅读(142)  评论(0编辑  收藏  举报

导航