数据结构-单向链表

单向链表

1、链表常见操作

(1)、append(element):
向列表尾部添加一个新的项
(2)、insert(position,element):
向列表的特定位置插入一个新的项
(3)、get(position):
获取对应位置的元素
(4)、indexOf(element):
返回元素在列表中的索引。如果列表中没有该元素则返回-1
(5)、update(position,element):
修改某个位置的元素
(6)、removeAt(position):
从列表的特定位置移除一项
(7)、remove(element):
从列表中移除一项
(8)、isEmpty():
如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false
(9)、size():
返回链表包含的元素个数。与数组的length属性类似。
(10)、toString():
由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法

2、封装单向链表
// 封装链表的构造函数
    function LinkedList() {
        // 封装一个Node类, 用于保存每个节点信息
        function Node(element) {
            this.element = element
            this.next = null
        }

        // 链表中的属性
        this.length = 0
        this.head = null

        // 1、append 链表尾部追加元素方法
        LinkedList.prototype.append = function (element) {
            // 1.根据新元素创建节点
            var newNode = new Node(element)

            // 2.判断原来链表是否为空
            if (this.head === null) { // 链表尾空
                this.head = newNode
            } else { // 链表不为空
                // 2.1.定义变量, 保存当前找到的节点
                var current = this.head
                while (current.next) {
                    current = current.next
                }

                // 2.2.找到最后一项, 将其next指向newNode
                current.next = newNode
            }

            // 3.链表长度增加1
            this.length += 1
        }

        //2、 链表的toString方法
        LinkedList.prototype.toString = function () {
            // 1.定义两个变量
            var current = this.head
            var listString = ""

            // 2.循环获取链表中所有的元素
            while (current) {
                listString += current.element + " "
                current = current.next
            }

            // 3.返回最终结果
            return listString
        }

        // 3、insert方法
        LinkedList.prototype.insert = function (position, element) {
            // 1.检测越界问题: 越界插入失败
            if (position < 0 || position > this.length) return false

            // 2.定义变量, 保存信息
            var newNode = new Node(element)
            var current = this.head
            var previous = null
            index = 0

            // 3.判断是否列表是否在第一个位置插入
            if (position == 0) {
                newNode.next = current
                this.head = newNode
            } else {
                while (index++ < position) {
                    previous = current
                    current = current.next
                }

                newNode.next = current
                previous.next = newNode
            }

            // 4.length+1
            this.length++

            return true
        }

        // 4、get方法
        LinkedList.prototype.get = function(position){
            // 1.越界判断
            if(position < 0 || position >= this.length) return null
            
            //2.获取对应的element
            var current = this.head
            var index = 0
            while(index++ < position){
                current = current.next
            }

            return current.element
        }

        //5、indexOf 根据元素获取链表中的位置
        LinkedList.prototype.indexOf = function (element) {
            // 1.定义变量, 保存信息
            var current = this.head
            index = 0

            // 2.找到元素所在的位置
            while (current) {
                if (current.element === element) {
                    return index
                }     
                current = current.next
                index += 1
            }

            // 3.来到这个位置, 说明没有找到, 则返回-1
            return -1
        }

        // 6、update方法
        LinkedList.prototype.update = function (position, newElement){
            //1.越界判断
            if(position < 0 || position > this.length) return false

            //2.查找正确的节点
            var current = this.head
            var index = 0
            while(index++ < position){
                current = current.next
            }

            //3.将position位置的node的element修改为newElement
            current.element = newElement
            
            return true
        }



        // 7、removeAt方法 根据位置移除节点
        LinkedList.prototype.removeAt = function (position) {
            // 1.检测越界问题: 越界移除失败, 返回null
            if (position < 0 || position >= this.length) return null

            // 2.定义变量, 保存信息
            var current = this.head
            var previous = null
            var index = 0

            // 3.判断是否是移除第一项
            if (position === 0) {
                this.head = current.next
            } else {
                while (index++ < position) {
                    previous = current
                    current = current.next
                }

                previous.next = current.next
            }

            // 4.length-1
            this.length -= 1

            // 5.返回移除的数据
            return current.element
        }


        // 8、根据元素删除信息
        LinkedList.prototype.remove = function (element) {
            var index = this.indexOf(element)
            return this.removeAt(index)
        }

        // 9、判断链表是否为空
        LinkedList.prototype.isEmpty = function () {
            return this.length == 0
        }

        // 10、获取链表的长度
        LinkedList.prototype.size = function () {
            return this.length
        }

         
    }

    // 测试链表
    // 1.创建链表
    var list = new LinkedList()

    // 2.追加元素
    list.append(15)
    list.append(10)
    list.append(20)
    alert(list) // 15 10 20

    // 3.测试insert方法
    list.insert(0, 100)
    list.insert(4, 200)
    list.insert(2, 300)
    alert(list) // 100 15 300 10 20 200

    // 4.测试get方法
    alert(list.get(0))
    alert(list.get(3))
    alert(list.get(5))

    // 5.测试indexOf方法
    alert(list.indexOf(15))
    alert(list.indexOf(10))
    alert(list.indexOf(20))
    alert(list.indexOf(100))

    // 6. 测试update方法
    alert(list.update(5, 999))
    alert(list)  // 100 15 300 10 20 999 

    // 7.测试removeAt方法
    list.removeAt(0)
    list.removeAt(1)
    list.removeAt(3)
    alert(list) // 15 10 20 

    // 8.测试remove方法
    list.remove(15)
    alert(list) // 10 20 

    // 8.测试其他方法
    alert(list.isEmpty())
    alert(list.size())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值