双链表的基本相关方法

双链表

***面试问题:***面试问题:数组和链表有什么区别? ArrayList和 LinkedList 的区别
顺序表:
优点: 空间连续,支持随机访问
缺点:1.中间或前面部分的插入删除时间复杂度O(N)
2.增容的代价比较大

单链表:
缺点:1、以节点为单位存储,不支持随机访问
优点:1、任意位置插入删除的时间复杂度为O(1) 头插O(1) ,尾插O(N) (必须找到为尾巴)
2、没有增容问题,插入一个开辟一个空间

class Node {
    public Node prev;
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}
public class MyDoubleList {
    public Node head ;
    public Node last ;
    //public Node head = new Node(-1);   傀儡节点
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if (this.head==null){
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if (this.last==null){
            this.head = node;
            this.last = node;
        }else {
            last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index<0||index>size()){
            System.out.println("index不合法!");
            return;
        }
        if (index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
//        Node cur = this.head;
//        while (index!=0){
//            cur=cur.next;
//            index--;
//        }
        Node cur = findIndexNode(index);
        Node node = new Node(data);
        node.next =cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    //找出index下标对应的节点
    private  Node findIndexNode(int index){
        Node cur = this.head;
        while (index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = this.head;
        while (cur!= null){
            if (cur.val!=key){
                cur = cur.next;
            }else {
                return  true;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        Node cur = this.head;
        while (cur!=null){
            if (cur.val==key){
                if (cur==this.head) {   // 如果删除头节点
                    this.head = cur.next;
                    if (this.head!=null){         //  如果只有头节点的情况 ,
                        this.head.prev = null;
                    }else {      //尾节点得置为null   否则会自己指向自己
                        this.last=null;          //一定不能忘记   否则会发生泄露
                    }
                }else {                         //优化后的代码  中间节点和尾节点写在了一起
                    cur.prev.next = cur.next;     //公共的代码
                    if (cur.next==null){
                        this.last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
//                }else if (cur==this.last){     //如果删除尾节点
//                    cur.prev.next=cur.next;
//                    this.last = cur.prev;
//                }else {
//                    cur.prev.next = cur.next;    // 删除中间节点
//                    cur.next.prev = cur.prev;   // 删除中间节点和尾节点有公共代码,可以优化
//                }
            }else {
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node cur = this.head;
        while (cur!=null){
            if (cur.val==key){
                if (cur==this.head) {   // 如果删除头节点
                    this.head = cur.next;
                    if (this.head!=null){         //  如果只有头节点的情况 ,
                        this.head.prev = null;
                    }else {      //尾节点得置为null   否则会自己指向自己
                        this.last=null;          //一定不能忘记   否则会发生泄露
                    }
                }else {                         //优化后的代码  中间节点和尾节点写在了一起
                    cur.prev.next = cur.next;     //公共的代码
                    if (cur.next==null){
                        this.last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
//                }else if (cur==this.last){     //如果删除尾节点
//                    cur.prev.next=cur.next;
//                    this.last = cur.prev;
//                }else {
//                    cur.prev.next = cur.next;    // 删除中间节点
//                    cur.next.prev = cur.prev;   // 删除中间节点和尾节点有公共代码,可以优化
//                }
            }
            cur = cur.next;        //放到外面   一直走下去   不管删除没删除 都得走下去
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        Node cur = this.head;
        while (cur!=null){
            count++;
            cur = cur.next;
        }
        return  count;
    }
    public void display(){
        Node cur = this.head;
        while (cur!=null){
            System.out.print(cur.val + " ");
            cur= cur.next;
        }
        System.out.println();
    }
    public void clear(){
        while (this.head!=null) {
            Node cur = this.head.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = cur;
        }
        this.last = null;
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值