双向链表

 class CNode {
            constructor(data) {
                this.prev = null;
                this.data = data;
                this.next = null;
            }
        }
        class DoubleLinkList {
            constructor() {
                this.head = null;
                this.tail = null;
                this.len = 0;
            }
            append(ele) {
                let newnode = new CNode(ele);
                if (this.len == 0) {
                    //空链表
                    this.head = newnode;
                    this.tail = newnode;
                } else {
                    newnode.prev = this.tail;
                    this.tail.next = newnode;
                    this.tail = newnode;
                }
                this.len++
            }
            insert(position, ele) {
                if (position < 0 || position > this.len || !Number.isInteger(position)) return false

                let newnode = new CNode(ele);

                if (position == 0) {
                    if (this.len == 0) {
                        this.head = newnode;
                        this.tail = newnode;
                    } else {
                        newnode.next = this.head;
                        this.head.prev = newnode;
                        this.head = newnode;
                    }
                    this.len++
                } else if (position == this.len) {
                    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;
                    newnode.next.prev = newnode;
                    this.len++
                }
            }
            removeAt(position) {
                if (position < 0 || position > this.len - 1 || !Number.isInteger(position)) return false

                if (position == 0) {
                    this.head = this.head.next;
                    this.head.prev = null
                } else if (position == this.len - 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.len--
            }
            indexOf(ele) {
                let current = this.head,
                    index = 0;
                while (index < this.len) {
                    if (current.data == ele) {
                        return index
                    } else {
                        current = current.next;
                        index++
                    }
                }
                return -1
            }

            remove(ele) {
                let in1 = this.indexOf(ele);
                this.removeAt(in1)
            }

            toAfterString() {
                let current = this.head, index = 0, res = "";

                while (index < this.len) {
                    res += '-' + current.data;
                    current = current.next;
                    index++
                }

                return res.slice(1)
            }

            toBeforeString() {
                let current = this.tail, index = this.len - 1, res = "";

                while (index >= 0) {
                    res += '-' + current.data;
                    current = current.prev;
                    index--
                }

                return res.slice(1)
            }
        }

        let dlist = new DoubleLinkList();
        for (let i = 0; i < 10; i++) {
            dlist.append(i)
        }

        dlist.remove(4)
        dlist.remove(8)

        dlist.insert(4, "hello")

        console.log(dlist.toAfterString());
        console.log(dlist.toBeforeString());

 双向链表

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

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

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

  • 双向链表有什么缺点呢?

  • 每次在插入或删除某个节点时, 需要处理四个节点的引用, 而不是两个. 也就是实现起来要困难一些

  • 并且相当于单向链表, 必然占用内存空间更大一些.

  • 但是这些缺点和我们使用起来的方便程度相比, 是微不足道的.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值