雙向鏈表的插入刪除新增

鏈表節點 存放 結點值,前繼結點,後續結點

public class DoubleLink {
    public Long value;
    public DoubleLink pre;
    public DoubleLink next;

    public DoubleLink(Long value) {
        this.value = value;
    }
    public void displayDoubleLink(){
        System.out.print(" "+value);
    }
}

鏈表對象,存放首結點 末節點

public class DoubleLinkList {
    private DoubleLink first;
    private DoubleLink last;

    public DoubleLinkList() {
        this.first = first;
        this.last = last;
    }

    public void insertFirst(long dd){
        DoubleLink newLink = new DoubleLink(dd);
        //把first節點的後置節點置爲當前節點,
        if(isEmpty()){//如果是空鏈表 需要把last指向末尾
            last=newLink;
        }else{
            first.pre=newLink;  //newLink <-- old first
        }
        newLink.next=first;
        first=newLink;

    }
    public void insertLast(long dd){
        DoubleLink newLink = new DoubleLink(dd);
        if(isEmpty()){
            first=newLink;
        }else{
            last.next=newLink;
            newLink.pre=last;
        }
        last=newLink;
    }

    public DoubleLink deleteFirst(){
        DoubleLink temp=first;
        if(first.next==null){ //if only one item
            last=null;       //null <-- last
        }else{
            first.next.pre=null;
        }
        first=first.next;
        return  temp;
    }

    public DoubleLink deleteLast(){
        DoubleLink temp=last;
        if(first.next==null){
            first=null;
        }else{
            last.pre.next=null;
        }
        last=last.pre;
        return temp;
    }


    public  boolean insertAfterKey(long key,long dd){
        DoubleLink curr=first;
        while (curr.value!=key){
            curr=curr.next;
            if(curr==null){
                return false; //didn't find it
            }
        }
        DoubleLink newLink=new DoubleLink(dd);
        if(curr==last){
            newLink.next=null;
            last=newLink;
        }else{
             newLink.next=curr.next;
             curr.next.pre=newLink;
        }
        newLink.pre=curr;
        curr.next=newLink;
        return true;
    }

    public DoubleLink deleteKey(long key){
        DoubleLink curr=first;
        while (curr.value!=key){
            curr=curr.next;
            if(curr.next==null){
                return null;
            }
        }
        if(curr==first){
            first=curr.next;
        }else{
            curr.pre.next=curr.next;
        }
        if(curr==last){
            last=curr.pre;
        }else{
            curr.next.pre=curr.pre;
        }
        return curr;
    }


    public void dispalyForward(){
        System.out.print("List(first--last):");
        DoubleLink curr=first;
        while (curr!=null){
            curr.displayDoubleLink();
            curr=curr.next;
        }
        System.out.println("");
    }

    public  void displayBackWord(){
        System.out.print("List (last-->first: ");
        DoubleLink curr=last;
        while (curr!=null){
            curr.displayDoubleLink();
            curr=curr.pre;
        }
    }
     public boolean  isEmpty(){
        return first==null;
     }
    public DoubleLink getFirst() {
        return first;
    }

    public void setFirst(DoubleLink first) {
        this.first = first;
    }

    public DoubleLink getLast() {
        return last;
    }

    public void setLast(DoubleLink last) {
        this.last = last;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章