JavaScript数据结构之双向链表的封装

AI 概述
JavaScript 数据结构之双向链表的封装 function DoublyLinkedList() { function Node(data) { this.next = null this.prev = null this.data = data } this.head = null this.tail = null this.length = 0 DoublyLinkedList.prototype.append = function(data) { var newNode = new No...

JavaScript 数据结构之双向链表的封装

function DoublyLinkedList() {
    function Node(data) {
        this.next = null this.prev = null this.data = data
    }
    this.head = null this.tail = null this.length = 0 DoublyLinkedList.prototype.append = function(data) {
        var newNode = new Node(data) if (this.length === 0) {
            this.head = newNode this.tail = newNode
        } else {
            newNode.prev = this.tail this.tail.next = newNode this.tail = newNode
        }
        this.length++
    }
    DoublyLinkedList.prototype.toString = function() {
        return this.forwardString()
    }
    DoublyLinkedList.prototype.backwardString = function() {
        var current = this.tail
        var listString = ''
        while (current) {
            listString += current.data + ''current = current.prev
        }
        return listString
    }
    DoublyLinkedList.prototype.forwardString = function() {
        var current = this.head
        var listString = ''
        while (current) {
            listString += current.data + ''current = current.next
        }
        return listString
    }
    DoublyLinkedList.prototype.insert = function(data, position) {
        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) {
                newNode.next = this.head this.head.prev = newNode this.head = newNode
            } else if (position == this.length) {
                newNode.prev = this.tail this.tail.next = newNode this.tail = newNode
            } else {
                var current = this.head
                var previous = null
                var index = 0
                while (index++<position) {
                    previous = current current = current.next
                }
                newNode.next = current current.prev = newNode previous.next = newNode newNode.prev = previous
            }
        }
        this.length++
    }
    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
    }
    DoublyLinkedList.prototype.indexOf = function(data) {
        var index = 0
        var current = this.head
        while (current) {
            if (current.data === data) {
                return index
            }
            current = current.next index++
        }
        return - 1
    }
    DoublyLinkedList.prototype.update = function(data, position) {
        if (position < 0 || position >= this.length) return false
        var index = 0
        var current = this.head
        while (index++<position) {
            current = current.next
        }
        current.data = data
        return true
    }
    DoublyLinkedList.prototype.removeAt = function(position) {
        if (position < 0 || position >= this.length) return null
        var index = 0
        var current = this.head
        var previous = null
        if (this.length === 1) {
            this.head = null this.tail = null
        } else {
            if (position === 0) {
                this.head = this.head.next this.head.next.prev = null
            } else if (position == this.length - 1) {
                current = this.tail this.tail = this.tail.prev this.tail.prev.next = null
            } else {
                while (index++<position) {
                    current = current.next
                }
                current.prev.next = current.next current.next.prev = current.prev
            }
        }
        this.length--
        return current.data
    }
    DoublyLinkedList.prototype.remove = function(data) {
        var position = this.indexOf(data) return this.removeAt(position)
    }
    DoublyLinkedList.prototype.isEmpty = function() {
        if (this.length) return true
        return false
    }
    DoublyLinkedList.prototype.size = function() {
        return this.length
    }
}

以上关于JavaScript数据结构之双向链表的封装的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。

「点点赞赏,手留余香」

0

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » JavaScript数据结构之双向链表的封装

发表回复