栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构-单向链表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构-单向链表

单向链表 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())
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659326.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号