首页 > 其他分享 >什么是双向链表?双向链表的操作封装实现(增删改查)?

什么是双向链表?双向链表的操作封装实现(增删改查)?

时间:2022-08-24 23:46:08浏览次数:71  
标签:current head index 改查 next 链表 length 双向 newnode

什么是双向链表?

双向链表

  • 既可以从头遍历到尾, 又可以从尾遍历到头

  • 也就是链表相连的过程是双向的. 那么它的实现原理, 你能猜到吗?

  • 一个节点既有向前连接的引用, 也有一个向后连接的引用.

  • 双向链表可以有效的解决单向链表中提到的问题.

 

 

双向链表的操作封装实现

        // 节点的结构   只能通过newnode调用
        class Dnode {
            constructor(data) {
                this.prev = null;
                this.data = data;
                this.next = null;
            }
        }
        // 链表的结构
        class DoubleLinkList {
            constructor() {
                this.head = null;
                this.tail = null
                this.length = 0;
            }

            // 1、向链表最后添加元素
            append(ele) {

                // 创建新节点
                let newnode = new Dnode(ele);
                // console.log(newnode);

                // 当头节点为空
                if (this.length == 0) {
                    this.head = newnode;
                    this.tail = newnode;
                } else {
                    newnode.prev = this.tail
                    this.tail.next = newnode;
                    this.tail = newnode
                }
                this.length++
            }

            // 2、向指定位置插入元素
            insert(position, ele) {
                // 位置是否合法
                if (position = 0 || position > this.length || !Number.isInteger(position)) {
                    return false
                }

                let newnode = new Dnode(ele)
                // 1、在头部插入

                if (position == 0) {
                    if (this.length == 0) {
                        this.head = newnode;
                        this.tail = newnode;
                    } else {
                        newnode.next = this.head;
                        this.head.prev = newnode
                        this.head = newnode
                    }
                    this.length++;
                } else if (position == this.length) {
                    //尾部
                    this.append(ele)
                } else {
                    let current = this.head,
                        index = 0

                    while (index < position - 1) {
                        current = current.next;
                        index++
                    }
                    newnode.prev = current;
                    newnode.next = current.next

                    current.next = newnode;
                    current.next.prev = newnode;
                    this.length++
                }

            }

            // 3、移除指定位置的元素
            removeAt(position) {
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                if (this.length == 0) {
                    return
                } else {
                    if (position == 0) {
                        if (this.length == 1) {
                            this.head = null
                            this.tail = null
                        } else {
                            this.head = this.head.next
                            this.head.prev = null
                        }
                        this.length--;
                    } else if (position == this.length - 1) {
                        this.tail = this.tail.prev
                        this.tail.next = null
                    } else {
                        let current = this.head, index = 0
                        while (index < position - 1) {
                            current = current.next;
                            index++
                        }

                        current.next = current.next.next
                        current.next.prev = current
                    }
                    this.length--
                }
            }

            // 4、查找指定元素的位置
            indexOf() {
                let current = this.head,
                    index = 0;
                while (index < this.length) {
                    if (current.data == ele) {
                        return index
                    } else {
                        //不相等时
                        current = current.next
                        index++;
                    }
                }
                return -1;
            }

            // 5、移动指定元素
            remove(ele) {
                let index = this.indexOf(ele);
                this.removeAt(index)
            }

            // 6、正向遍历
            toAfterString() {
                let current = this.head,
                    index = 0;
                while (index < this.length) {
                    res += "-" + current.data;
                    current = current.next
                    index++
                }
                return res.slice(1)
            }

            // 7、反向遍历
            toBeforeString() {
                let current = this.tail,
                    index = this.length - 1;
                while (index < this.length) {
                    res += "-" + current.data;
                    current = current.next
                    index++
                }
                return res.slice(1)
            }

        }
        let dlist = new DoubleLinkList();
        for (let i = 0; i < 5; i++) {
            list.append(i)
        }
        console.log(dlist);

 

标签:current,head,index,改查,next,链表,length,双向,newnode
From: https://www.cnblogs.com/LIXI-/p/16622697.html

相关文章

  • 合并两条有序链表
     用JavaScript实现:constlink1=newLinkedList()link1.append(1)link1.append(3)link1.append(4)link1.append(6)li......
  • 双链表和循环链表
    一、结构体定义1.双链表typedefstructDLNode{ intdata; structDLNode*prior,*next;}DLNode;2.循环链表//同双链表二、操作1.尾插法建立双链表voidcreat......
  • LeetCode 重排链表算法题解 All In One
    LeetCode重排链表算法题解AllInOnejs/ts实现重排链表重排链表原理图解//快慢指针重排链表https://leetcode.com/problems/reorder-list/https://le......
  • LeetCode 21 合并两个有序链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListN......
  • pyhton单表数据增删改查
    fromdjango.testimportTestCase#Createyourtestshere.importosif__name__=='__main__':os.environ.setdefault('DJANGO_SETTINGS_MODULE','DAY64.settings......
  • mysql增删改查json中的某个字段
    创建表1CREATETABLEt_json(idINTPRIMARYKEY,NAMEVARCHAR(20),infoJSON);插入记录1INSERTINTOt_json(id,sname,info)VALUES(1,'test','{"time":"20......
  • LeetCode 142. 环形链表 II
    思路:快慢指针法:当快指针与慢指针相遇时,分别从起点,相遇点开始走,相遇即为环入口/***Definitionforsingly-linkedlist.*structListNode{*intval;*......
  • LeetCode 24. 两两交换链表中的节点
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*List......
  • 原生小程序的单向双向绑定--简易双向绑定
    1、在wxml中,普通属性的数据绑定是单向的<inputvalue="{{value}}"/>上面这串代码,可以通过this.setData({value:xxx})去更新值和输入框中显示的值也会更新为xxx,但是如果......
  • LeetCode 206. 反转链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListN......