双链表的设计

这篇博客详细介绍了如何实现一个双向链表类`MyLinkedList`,包括在头部和尾部添加元素、在指定位置插入元素以及删除指定位置的元素等操作。博客还探讨了优化索引查找的方法,如根据索引位置选择从头或尾开始遍历以提高效率。此外,代码中强调了初始化链表时头尾节点的正确连接方式。
摘要由CSDN通过智能技术生成
// public class List{
//     public static void main(String[] args){
//         MyLinkedList linkedList = new MyLinkedList();
//         linkedList.addAtHead(1);
//         linkedList.addAtTail(3);
//         linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
//         linkedList.get(1);            //返回2
//         linkedList.deleteAtIndex(1);  //现在链表是1-> 3
//         linkedList.get(1);            //返回3
//     }
// }



class DoubleListNode{
    int val;
    DoubleListNode next;
    DoubleListNode prev;

    public DoubleListNode() {
    }

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

class MyLinkedList {
    int size;
    DoubleListNode head;
    DoubleListNode tail;

    public MyLinkedList() {
        size = 0;
        head = new DoubleListNode();
        tail = new DoubleListNode();
        //这里要注意,初始化的时候,双向链表的头尾是相连的。并且head的prev为null,tail的next为null。
        head.next = tail;
        tail.prev = head;
    }

    public int get(int index) {
        if(index < 0 || index >=size){
            return -1;
        }

        DoubleListNode cur = new DoubleListNode();

        if(index < (size - 1)/2){
            cur = head;
            //从头开始走到索引为Index的结点处,要走index+1步
            for(int i = 0;i < (index+1); i ++){
                cur = cur.next;
            }
        }
        if(index >= (size-1)/2){
            cur = tail;
            //从尾巴开始走,走到索引为index处的结点,要走size-index步
            for(int i = 0;i < (size-index);i ++){
                cur = cur.prev;
            }
        }
        return cur.val;
    }

    public void addAtHead(int val) {
        size++;
        DoubleListNode tooAdd = new DoubleListNode(val);
        tooAdd.next = head.next;
        head.next.prev = tooAdd;
        head.next = tooAdd;
        tooAdd.prev = head;
    }

    public void addAtTail(int val) {
        size++;
        DoubleListNode tooAdd = new DoubleListNode(val);
        tooAdd.prev = tail.prev;
        tail.prev.next = tooAdd;
        tooAdd.next = tail;
        tail.prev = tooAdd;
    }

    public void addAtIndex(int index, int val) {
        if(index > size){
            return;
        }
        DoubleListNode tooAdd = new DoubleListNode(val);
        DoubleListNode before = head;
        size ++;

        if(index < 0){
            index = 0;
        }

        for(int i = 0;i < index ; i ++){
            before = before.next;
        }
        tooAdd.next = before.next;
        before.next.prev = tooAdd;
        before.next = tooAdd;
        tooAdd.prev = before;
    }

    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size){
            return;
        }

        DoubleListNode before = head;
        size --;

        for(int i = 0;i < index ; i ++){
            before = before.next;
        }

        before.next.next.prev = before;
        before.next = before.next.next;

    }
}

//        //这里要注意,初始化的时候,双向链表的头尾是相连的。并且head的prev为null,tail的next为null。
//        head.next = tail;
//        tail.prev = head;


//注意依靠next指针寻找到的结点要放到最后面去交换。
//        tooAdd.next = before.next;
//        before.next.prev = tooAdd;
//        before.next = tooAdd;
//        tooAdd.prev = before;



//            //从头开始走到索引为Index的结点处,要走index+1步
//            for(int i = 0;i < (index+1); i ++){

//            //从尾巴开始走,走到索引为index处的结点,要走size-index步
//            for(int i = 0;i < (size-index);i ++){
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值