数据结构之双向链表

双向链表,跟单向链表比起来,多了previous跟tail,prev主要用于给当前节点指向上一个节点,tail类比head,用于标记最后一个节点,tail的next指向null

双向链表如图

代码

    class Node {

        constructor(data) {

            this.element = data

            this.previous = null

            this.next = null

        }

    }

    class DoublyLinkedList {

        constructor() {

            this.length = 0

            this.head = null

            this.tail = null

        };

        append(element) {

            let newNode = new Node(element)

            //双向链表的插入

            if (this.length == 0) {

                this.head = newNode

                this.tail = newNode

            } else {

                this.tail.next = newNode

                newNode.previous = this.tail

                this.tail = newNode

            }

            this.length += 1

        };

        forwardString() {

            let current = this.head;

            let forwardStr = ''

            while (current) {

                forwardStr += current.element + " "

                current = current.next

            }

            return forwardStr

        };

        reverseString() {

            let current = this.tail

            let reverseStr = ''

            while (current) {

                reverseStr += current.element + " "

                current = current.previous

            }

            return reverseStr

        }

        toString() {

            return this.forwardString()

        };

        insert(position, element) {

            if (position < 0 || position > this.length) return false

            var newNode = new Node(element)

            if (position == 0) {

                if (this.length == 0) {

                    this.head = newNode

                    this.tail = newNode

                } else {

                    this.head.previous = newNode

                    newNode.next = this.head

                    this.head = newNode

                }

            } else if (this.length == position) {

                //尾部插入

                newNode.previous = this.tail

                this.tail.next = newNode

                this.tail = newNode

            } else {

                let [index, current, prev] = [0, this.head, null]

                //0 2

                while (index++ < position) {

                    prev = current

                    current = current.next

                }

                newNode.next = current

                newNode.previous = prev

                current.previous = newNode

                prev.next = newNode

            }

            this.length++

            return true

        };

        removeAt(position, element) {

            if (position < 0 || position > this.length) return false

            // 2.判断移除的位置

            var current = this.head

            if (position === 0) {

                if (this.length == 1) {

                    this.head = null

                    this.tail = null

                } else {

                    this.head = this.head.next

                    this.head.prev = null

                }

            } else if (position === this.length - 1) {

                current = this.tail

                this.tail = this.tail.prev

                this.tail.next = null

            } else {

                var index = 0

                var previous = null

                while (index++ < position) {

                    previous = current

                    current = current.next

                }

                previous.next = current.next

                current.next.prev = previous

            }

            // 3.length-1

            this.length--

            return current.element

        }

    }

    let doublyLinkedList = new DoublyLinkedList()

    doublyLinkedList.append("aaa")

    doublyLinkedList.append("bbb")

    doublyLinkedList.append("ccc")

    doublyLinkedList.append("ddd")

    doublyLinkedList.append("eee")

    doublyLinkedList.append("fff")

    doublyLinkedList.insert(6, "push")

    doublyLinkedList.insert(0, 'l')

    doublyLinkedList.insert(2, 'j')

    doublyLinkedList.insert(2, 'd')

    console.log(doublyLinkedList.toString())//l aaa d j bbb ccc ddd eee fff 

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 双向链表和普通链表的区别是,普通链表中一个节点只有一个next指针指向下一个节点,双向链表有2个指针,一个指向下一...
    安然_她阅读 207评论 0 1
  • 如需转载, 请咨询作者, 并且注明出处.有任何问题, 可以关注我的微博: coderwhy, 或者添加我的微信: ...
    coderwhy阅读 7,925评论 1 30
  • 数据结构与算法 入门篇复杂度分析时间复杂度大O时间复杂度表示法,表示代码执行时间随数据规模增长的变化趋势,也叫渐进...
    xinmin阅读 180评论 0 0
  • 一. 认识双向链表 双向链表介绍 单向链表: 我们可以轻松的到达下一个节点, 但是回到钱一个节点是很难的. 但是,...
    小码哥教育520it阅读 15,235评论 0 0
  • 链表是什么及链表相对于数组的优缺点 跟数组比起来优点,插入跟删除的性能高很多,因为不会改变其它node;缺点是查找...
    aufhebung阅读 60评论 0 0