链表的插入、删除、查找

//创建链表
class DoubleList{
    public int val;
    public DoubleList(int val) {
        this.val = val;
    }
    public DoubleList() {

    }
    public DoubleList pre;
    public DoubleList next;
}


//对进行操作链表的方法具体实现
public class DoubleLinkedList {
    public DoubleList head;
    public DoubleList last;

    //头插法
    public void addFirst(int data){
        DoubleList node = new DoubleList(data);
        if(head == null) {
            head = node;
            last = node;
        }

        node.next = this.head;
        this.head.pre = node;
        head = node;
    }


    //尾插法
    public void addLast(int data){
        DoubleList node = new DoubleList(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else{
            last.next = node;
            node.pre = last;
            last = node;
        }
    }


    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        DoubleList cur = new DoubleList(data);
        DoubleList node = this.head;
        if(index == 0){
            cur.next = head;
            head.pre = cur;
            head = cur;
            return true;
        }
        if(index == size()){
            cur.next = last;
            last.pre.next = cur;
            last.pre = cur;
            return true;
        }
        int count = 1;
        if(size() < index || index < 0) return false;
        while (node.next != null && count < index){
            count++;
            node = node.next;
        }
        cur.next = node.next;
        node.next.pre = cur;
        node.next = cur;
        cur.pre = node;
        return true;
    }


    //查找是否包含关键字key是否在链表当中
    public boolean contains(int key){
        DoubleList node = this.head;
        if(this.head == null) return false;
        while(node != null){
            if(node.val == key){
                return true;
            }else {
                node = node.next;
            }
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key){
        DoubleList node = this.head;
        if(this.head == null) return;
        while(node != null){
            if(node.val == key){
                //删除节点是头节点
               if(node == this.head){
                   this.head = this.head.next;
                   this.head.pre = null;
               }else{
                   //删除节点是中间节点
                   node.pre.next = node.next;
                   if(node.next != null){
                       node.next.pre = node.pre;
                   }else{
                       //删除尾巴节点
                       last = node.pre;
                   }
               }
               return;
            }else {
                node = node.next;
            }
        }
    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        DoubleList node = this.head;
        while (node != null){
            if(node.val == key){
                //node节点为要删除的节点
                if(node == this.head){
                    this.head = this.head.next;
                    //假设全部是要删除的数字
                    if (this.head != null){
                        this.head.pre = null;
                    }else{
                        last = null; //防止链表的节点不能被回收
                    }
                }else{
                    //删除节点要么在中间,要是尾巴节点
                    node.pre.next = node.next;
                    if(node.next != null){
                        node.next.pre = node.pre;
                    }else {
                        //删除尾巴节点
                        last = node.pre;

                    }
                }
                node = node.next;
            }else {
                node = node.next;
            }
        }
    }


    //得到单链表的长度
    public int size(){
        DoubleList node = this.head;
        int count = 0;
        while (node != null){
            count++;
            node = node.next;
        }
        return count;
    }

    //输入链表每个节点的值
    public void display(){
        DoubleList node = this.head;
        if (node == null) return;
        while (node != null){
            System.out.print(node.val+" ");
            node = node.next;
        }
        System.out.println();
    }
    
    
    //清空链表所有节点
    public void clear(){
        while (this.head != null){
            this.head = this.head.next;
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matinal_01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值