JavaScript数据结构与算法Day7(双向链表的封装)

149 阅读1分钟

双向链表封装

基本结构

function doublyLinkedList() {
            function Node(data) {
                this.data = data
                this.next = null
                this.prev = null
            }
            this.length = 0
            this.head = null
            this.tail = null
        }

append方法

doublyLinkedList.prototype.append = function (data) {
                var newNode = new Node(data)
                if (this.length == 0) {
                    this.head = newNode
                    this.tail = newNode
                } else {
                    this.tail.next = newNode
                    newNode.prev = this.tail
                    this.tail = newNode
                }
                this.length += 1
            }

toString方法

doublyLinkedList.prototype.toString = function () {
                return this.backwardtoString()
            }

backwardtoString方法

doublyLinkedList.prototype.backwardtoString = function () {
                var current = this.head
                var res = ''
                while (current) {
                    res += current.data + ' '
                    current = current.next
                }
                return res
            }

forwardtoString方法

doublyLinkedList.prototype.forwardtoString = function () {
                var current = this.tail
                var res = ''
                while (current) {
                    res += current.data + ' '
                    current = current.prev
                }
                return res
            }

insert方法

doublyLinkedList.prototype.insert = function (position, data) {
                if (position < 0 || position > this.length) return false
                var newNode = new Node(data)
                if (this.length == 0) {
                    this.head = newNode
                    this.tail = newNode
                } else {
                    if (position == 0) {
                        this.head.prev = newNode
                        newNode.next = this.head
                        this.head = newNode
                    } else if (position == this.length) {
                        this.tail.next = newNode
                        newNode.prev = this.tail
                        this.tail = newNode
                    } else {
                        var current = this.head
                        var index = 0
                        while (index++ < position) {
                            current = current.next
                        }
                        newNode.next = current
                        newNode.prev = current.prev
                        current.prev.next = newNode
                        current.prev = newNode
                    }
                }
                this.length += 1
            }

get方法

doublyLinkedList.prototype.get = function (position) {
                if (position < 0 || position >= this.length) return null
                var index = 0
                var current = this.head
                while (index++ < position) {
                    current = current.next
                }
                return current.data
            }

indexOf方法

doublyLinkedList.prototype.indexOf = function (data) {
                var current = this.head
                var index = 0
                while (current) {
                    if (current.data == data) {
                        return index
                    }
                    index += 1
                    current = current.next
                }
                return -1
            }

update方法

doublyLinkedList.prototype.update = function (position, newData) {
                if (position < 0 || position >= this.length) return false
                var current = this.head
                var index = 0
                while (index++ < position) {
                    current = current.next
                }
                current.data = newData
                return true
            }

removeAt方法

doublyLinkedList.prototype.removeAt = function (position) {
                if (position < 0 || position >= this.length) return null
                var current = this.head
                var index = 0
                if (this.length == 1) {
                    this.head = null
                    this.tail = null
                } else {
                    if (position == 0) {
                        this.head.next.prev = null
                        this.head = this.head.next
                    } else if (position == this.length - 1) {
                        this.tail.prev.next = null
                        this.tail = this.tail.prev
                    } else {
                        while (index++ < position) {
                            current = current.next
                        }
                        current.prev.next = current.next
                        current.next.prev = current.prev
                    }
                }
                return current.data
            }

remove方法

doublyLinkedList.prototype.remove = function (data) {
                var index = this.indexOf(data)
                this.removeAt(index)
                return true
            }

isEmpty方法

doublyLinkedList.prototype.isEmpty = function () {
                return this.length == 0
            }

size方法

doublyLinkedList.prototype.size = function () {
                return this.length
            }

getHead方法

doublyLinkedList.prototype.getHead = function () {
                return this.head.data
            }

getTail方法

doublyLinkedList.prototype.getTail = function () {
                return this.tail.data
            }