双向链表的插入删除新增

链表节点 存放 结点值,前继结点,后续结点

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值