js封装单向链表类

// 封装链表类
        function LinkedList() {
            // 内部的类:节点类
            function Node(data) {
                this.data = data
                this.next = null
            }
            // 属性
            this.head = null
            this.length = 0
            // 1.追加方法
            LinkedList.prototype.append = function (data) {
                //1.创建新的接点
                var newNode = new Node(data)
                //2.判断是否添加的是第一个节点
                if (this.length == 0) {
                    this.head = newNode
                } else {
                    // 找到最后一个接点
                    var current = this.head
                    while (current.next) {
                        current = current.next
                    }
                    // 最后节点next只想新的节点
                    current.next = newNode
                }
                //3.length++
                this.length += 1
            }
            // 2.toString方法
            LinkedList.prototype.toString = function () {
                // 1.定义变量
                var current = this.head
                var listString = ""
                // 2.循环获取一个个接点
                while (current) {
                    listString += current.data + " "
                    current = current.next
                }
                return listString
            }
            // 3.insert方法
            LinkedList.prototype.insert = function (position, data) {
                // 1.对position进行越界判断
                if (position < 0 || position > this.length) return false
                // 2.根据data创建newNode
                var newNode = new Node(data)
                // 3.判断插入的位置是否是第一个
                if (position == 0) {
                    newNode.next = this.head
                    this.head = newNode
                } else {
                    var index = 0
                    var current = this.head
                    var previous = null
                    while (index++ < position) {
                        previous = current
                        current = current.next
                    }
                    newNode.next = current
                    previous.next = newNode
                }
                this.length += 1
                return true
            }
            // 4.get方法
            LinkedList.prototype.get = function (position) {
                // 1.越界判断
                if (position < 0 || position >= this.length) return null
                // 2.获取对应的data
                var current = this.head
                var index = 0
                while (index < position) {
                    current = current.next
                }
                return current.data
            }
            LinkedList.prototype.indexOf = function (data) {
                // 定义变量
                var current = this.head
                var index = 0
                // 开始查找
                while (current) {
                    if (current.data == data) {
                        return index
                    }
                    current = current.next
                    index++
                }
                // 没有找到,返回-1
                return -1
            }
            LinkedList.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
                }
                // 将position位置的node的data修改为 newData
                current.data = newData
                return true
            }
            LinkedList.prototype.removeAt = function (position) {
                // 越界判断
                if (position < 0 || position >= this.length) return false
                // 判断是否删除的是第一个节点
                if (position == 0) {
                    this.head = this.head.next
                } else {
                    var index = 0
                    var current = this.head
                    var previous = null
                    while (index++ < position) {
                        previous = current
                        current = current.next
                    }
                    previous.next = current.next
                }
                this.length -= 1
                return current.data
            }
            LinkedList.prototype.remove = function (data) {
                // 根据data获取data在列表中的位置
                var position = this.indexOf(data)
                // 根据位置信息,删除节点
                return this.removeAt(position)
            }
            LinkedList.prototype.isEmpty = function () {
                return this.length == 0
            }
            // size()方法
            LinkedList.prototype.size = function () {
                return this.length
            }
        }

        var list = new LinkedList()
        list.append('abc')
        list.append('342')
        list.append('hhdf')
        list.insert(0, 'aaa')
        list.insert(3, 'nnn')
        console.log(list.get(0))
        console.log(list.indexOf('342'))
        list.update(2, '111')
        console.log(list)
        console.log(list.remove('abc'))

 

posted @ 2020-06-04 22:33  问问大将军  阅读(373)  评论(0编辑  收藏  举报