自己实现一个双向链表

自己实现一个双向链表

双向链表和单向链表不同的是双向链表中不仅有next指向, 还有pre这个指向, 分别指向了该节点的前一个节点和后一个节点, 这样比单向链表操作起来更方便.


class TwoNode{
    int val;
    TwoNode pre;
    TwoNode next;

    public TwoNode(int val){
        this.val = val;
    }
}

public class MyLinkedList {
    //双向链表需要记录收尾两个节点
    private TwoNode head;
    private TwoNode tail;
    private int length = 0;

    public MyLinkedList(){
        head = null;
        tail = null;
    }

    // 0. 求链表长度
    public int length(){
        return this.length;
    }

    // 1. 头插
    public void addFirst(int val){
        TwoNode newNode = new TwoNode(val);
        //空链表的情况
        if(head == null){
            head = newNode;
            tail = newNode;
            length++;
            return;
        }
        //非空链表
        newNode.next = head;
        head.pre = newNode;
        head = newNode;
        length++;
        return;
    }
    // 2. 尾插
    public void addLast(int val){
        TwoNode newNode = new TwoNode(val);
        //空链表情况
        if(head == null){
            head = newNode;
            tail = newNode;
            length++;
            return;
        }
        //非空链表
        tail.next = newNode;
        newNode.pre = tail;
        tail = newNode;
        length++;
        return;
    }
    // 3. 任意位置插入元素
    public void add(int index, int val){
        if(index < 0 || index > length){
            return;
        }
        if(index == 0){
            addFirst(val);
            return;
        }
        else if(index == length){
            addLast(val);
            return;
        }
        TwoNode newNode = new TwoNode(val);
        //一般情况需要先找到下标为index的节点
        TwoNode nextNode = getNode(index);
        TwoNode preNode = nextNode.pre;
        preNode.next = newNode;
        newNode.pre = preNode;
        nextNode.pre = newNode;
        newNode.next = nextNode;
        length++;
        return;
    }
    // 4. 头删
    public void deleteFirst(){
        if(head == null){
            return;
        }
        else if(head.next == null){
            head = null;
            tail = null;
            length--;
            return;
        }
        TwoNode nextNode = head.next;
        nextNode.pre = null;
        head = nextNode;
        length--;
        return;
    }
    // 5. 尾删
    public void deleteLast(){
        if(head == null){
            return;
        }
        else if(head.next == null){
            head = null;
            tail = null;
            length--;
            return;
        }
        TwoNode preNode = tail.pre;
        preNode.next = null;
        length--;
        return;
    }
    // 6. 按值删
    public void deleteByValue(int val){
        //可以找到该值对应的下标使用下标删除
        int index = indexOf(val);
        if(index == -1){
            return;
        }
        deleteByIndex(index);
        return;
    }
    // 7. 按下标删
    public void deleteByIndex(int index){
        if(index < 0 || index >= length){
            return;
        }
        if(index == 0){
            //头删
            deleteFirst();
            return;
        }
        if(index == length-1){
            deleteLast();
            return;
        }
        TwoNode cur = getNode(index);
        TwoNode preNode = cur.pre;
        TwoNode nextNode = cur.next;
        preNode.next = nextNode;
        nextNode.pre = preNode;
        length--;
        return;
    }
    // 8. 修改元素
    public void set(int index, int val){
        if(index < 0 || index >= length){
            return;
        }
        TwoNode toSet = getNode(index);
        toSet.val = val;
        return;
    }
    // 9. 查找元素
    public int indexOf(int value){
        TwoNode cur = head;
        for(int i = 0; i < length; i++){
            if(cur.val == value){
                return i;
            }
            cur = cur.next;
        }
        return -1;
    }
    // 10. 查找节点
    private TwoNode getNode(int index) {
        if(index < 0 || index > length){
            return null;
        }
        TwoNode cur = head;
        for(int i = 0; i < index; i++){
            cur = cur.next;
        }
        return cur;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值