美文网首页
JavaScript数据结构:单向链表

JavaScript数据结构:单向链表

作者: 再见噜噜班 | 来源:发表于2019-12-19 21:20 被阅读0次

定义

单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;列表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向NULL的指针。


单向链表图示.png

JavaScript实现

    function LinkedList() {
        function Node(data) {
            this.data = data
            this.next = null
        }
        this.head = null
        this.length = 0

        //append:向列表尾部添加一个项
        LinkedList.prototype.append = function (element) {
            //判断head是否为空
            if (this.head === null) {
                this.head = new Node(element)
            } else {
                //找到next为空的节点
                let current = this.head
                while (current.next != null) {
                    current = current.next
                }
                current.next = new Node(element)
            }
            this.length++
        }

        //insert(position,element):向列表的特定位置插入一个新的项
        LinkedList.prototype.insert = function (position, element) {
            if(position<0 || position>this.length) return false
            let node = new Node(element)
            if(position === 0){
                let head = this.head
                this.head = node
                node.next = head
            }else{
                let current = this.head
                //找到对应位置的前一项
                while(position>1){
                    current = current.next
                    position -- 
                }
                //获取当前项的原始next值
                let pre = current.next
                //让当前项的next指向插入的节点
                current.next = node
                //让插入的节点的next值指向原始的next节点
                node.next = pre
            }
            this.length++
            return true
        }

        //get(position):获取对应位置的元素
        LinkedList.prototype.get = function(position){
            if(position<0 || position >this.length-1) return false
            let current = this.head
            while(position-->0){
                current = current.next
            }
            return current.data
        }

        //indexOf(element):返回元素在列表中的索引。如果列表中没有则返回-1
        LinkedList.prototype.indexOf = function(element){
            let current = this.head
            let idx = 0
            while(current){
                if(current.data===element){
                    return idx
                }else{
                    current = current.next
                    idx++
                }
            }
            return -1
        }

        //removeAt(position):从列表的特定位置移除一项
        LinkedList.prototype.removeAt=function(position){
            if(position<0 || position>=this.length) return false
            if(position===0){
                this.head = this.head.next 
            }else{
                let current = this.head
                while(position>1){
                    current = current.next
                    position -- 
                }
                current.next = current.next.next
            }
            this.length--
        }

        //remove(element):从列表中移除一项
        LinkedList.prototype.remove=function(element){
            let idx = this.indexOf(element)
            return this.removeAt(idx)
        }

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

        //size():返回链表中元素的个数
        LinkedList.prototype.size = function(){
            return this.length
        }

        //toString():字符串表达
    }



    let l = new LinkedList()
    l.append(1)
    l.append(3)
    l.append(11)
    l.insert(2,5)
    l.insert(3,7)
    l.insert(5,0)
    l.removeAt(3)
    console.log(l)
单向链表insert算法图解.png

相关文章

  • 算法与数据结构知识汇总(二、链表)

    1、概念 2、链表的数据结构 单向链表的数据结构如下图: 上图数据结构为单向链表,简称单链表,该数据结构由若干个节...

  • 2019-12-04 Java-LinkedList源码解读

    @TOC 1、链表数据结构 链表分为单向链表和双向链表,他们的区别在于,单向链表只能单向寻址,而双向链表可以双向寻...

  • 用Java写单向链表

    数据结构—单向链表 为了巩固自己的基础知识,这次就用 Java 来写一个单向链表。问:什么是单向链表?首先链表是数...

  • 数据结构 | 其二 链表

    冰河winner - 数据结构之链表 2.1 单向链表 数据结构(一) 单链表的实现-JAVA 2.2 双端链表 ...

  • 线性表-单向循环链表

    单向循环链表 单向循环链表示意图如下: 数据结构定义(同普通链表) 单向循环链表初始化与赋值 在上面循环遍历查找尾...

  • JavaScript数据结构:单向链表

    定义 单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是...

  • 数据结构笔记

    数据结构课程概览 ================== 1.顺序表 2.链表:单链表,单向循环链表,双链表...

  • 链表反转

    概述 链表反转是非常经典的面试题,要实现此功能,需先实现链表的数据结构。 链表类 获得单向链表方法 输出单向链表方...

  • 总结

    Android篇 数据结构与算法顺序表 - ArrayList源码链表 - 单向链表、双向链表 - LinkedL...

  • 2018-03-26

    数据结构:单向链表 #include#include//实现一个简单的单向链表 (不带头节点的链表,只有一个头指针...

网友评论

      本文标题:JavaScript数据结构:单向链表

      本文链接:https://www.haomeiwen.com/subject/qgpvnctx.html