链表_双端双向链表(插入删除遍历)

双向链表不一定是双端的,不用必须有last节点

//链结点
public class Link {
    public long dData;
    public Link next;
    public Link previous;
    public Link(long d) {
        dData=d;    
    }
    public void displayLink() {
        System.out.print(dData+" ");
    }

}
public class DoublyLinkedList {
    private Link first;
    private Link last;
    public DoublyLinkedList() {
        first=null;
        last=null;
    }
    //判断是否为空
    public boolean isEmpty() {
        return first==null;
    }
    //从first插入
    public void insertFirst(long dd) {
        Link newLink=new Link(dd);
        if(isEmpty()) {
            last=newLink;
        }else
            first.previous=newLink;
        newLink.next=first;
        first=newLink;
    }
    //从last处增加
    public void insertLast(long dd) {
        Link newLink=new Link(dd);
        if(isEmpty()) {
            first=newLink;
        }else {
            last.next=newLink;
            newLink.previous=last;
        }
        last=newLink;
            
    }
    //从first删除
    public Link deleteFirst() {
        Link temp=first;
        if(first.next==null) {
            last=null;
        }else
            first.next.previous=null;
        first=first.next;
        return temp;
    }
    //从last删除
    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;
        
        
    }
    //delete key
    public Link deleteKey(long key) {
        Link current=first;
        //找到key的位置
        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.print("List(first-->last):");
        Link current=first;
        while(current!=null) {
            current.displayLink();
            current=current.next;
        }
        System.out.println();
    }
    //从后向前
    public void displayBackward() {
        System.out.print("List(last-->first):");
        Link current=last;
        while(current!=null) {
            current.displayLink();
            current=current.previous;
        }
        System.out.println();
    }

}

 

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DoublyLinkedList theList=new DoublyLinkedList();
        theList.insertFirst(22);
        theList.insertFirst(44);
        theList.insertFirst(66);
        theList.insertLast(11);
        theList.insertLast(33);
        theList.insertLast(55);
        theList.displayForward();
        theList.displayBackward();
        theList.deleteFirst();
        theList.deleteLast();
        theList.deleteKey(11);
        theList.displayForward();
    

    }

}

 

posted @ 2017-12-19 18:17  S-Mustard  阅读(346)  评论(0编辑  收藏  举报