Java实现LinkList双向列表

/**
 * 模拟实现LinkList
 */

public class MyLinkList {
    private class ListNode{
        int val;
        ListNode next;
        ListNode prev;
        ListNode(int val){
            this.val = val;
        }
    }
    private ListNode head;
    private ListNode tail;

    public MyLinkList() {
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //链表长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //判断链表中是否包含某元素
    public boolean contain(int val){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        if(head == null){
            head = new ListNode(data);
            tail = head;
            return;
        }
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }

    //尾插法
    public void addLast(int data){
        if(head == null){
            head = new ListNode(data);
            tail = head;
            return;
        }
        ListNode newNode = new ListNode(data);
        tail.next = newNode;
        newNode.prev = tail;
        tail = newNode;
    }


    //指定位置插入元素 head逻辑下标为0
    public void insert(int index,int data){
        //先判断index插入的位置是否合法
        if(index < 0 || index > size()){
            throw new IndexException("index 插入位置不合法!");
        }
        //头插
        if(index == 0){
            addFirst(data);
            return;
        }
        //尾插
        if(index == size()){
            addLast(data);
            return;
        }
        //常规插入
        ListNode cur = head;
        for (int i = 0;i < index;i++){
            cur = cur.next;
        }
        ListNode newNode = new ListNode(data);
        newNode.next = cur;
        newNode.prev = cur.prev;
        cur.prev.next = newNode;
        cur.prev = newNode;
    }

    //删除第一次出现的关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                //头结点为要删除的节点
                if(cur == head){
                    //只有一个头节点
                    if(head.next == null){
                        head = null;
                        tail = null;
                        return;
                    }
                    head = head.next;
                    head.prev = null;
                    return;
                }
                //尾节点为要删除的节点
                if(cur == tail){
                    tail = tail.prev;
                    tail.next = null;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;

        }
    }

    //删除所有关键字为key的节点
    public void removeAll(int key){
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                if(cur != tail){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }else {
                    tail = tail.prev;
                    tail.next = null;
                }
            }
            cur = cur.next;
        }
        if(head.val == key){
            //只有一个头结点
            if(head.next == null){
                head = null;
                tail = null;
            }else{
                //还有后继节点
                head = head.next;
                head.prev = null;
            }
        }
    }

    //清空链表
    public void clean(){
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        //释放头尾结点
        head = null;
        tail = null;
    }
}

异常类:

public class IndexException extends RuntimeException{
    public IndexException() {
    }

    public IndexException(String message) {
        super(message);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小小小关同学

你的支持就是我的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值