• 数据结构与算法之链表-javascript实现


    链表的定义:

    链表是一种物理存储单元上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。
    使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体磁盘上顺序,数据的存取往往要在不同的排列顺序中转换。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。链表有很多种不同的类型:单向链表双向链表以及循环链表。链表可以在多种编程语言中实现。
     
     
    下文中链表的实现原理是:
    每添加一个节点,就相当于实例化一个node对象,
    下一个节点是在当前node实例化对象的内部创建一个node对象,
    然后用current = current.next让指针永远指向链表的最后一项
    'use strict'
    
    function LinkedList(){
        var Node = function(ele){
            this.element = ele;
            this.next = null;
        };
        this.length = 0;
        this.head = null;
    /*向链表末尾添加一个元素*/
    this.append = function(ele){ var node = new Node(ele), current; if(this.head == null){ this.head = node; }else{ current = this.head; while(current.next){ current = current.next; } current.next = node; } this.length++; };
    /*通过索引从链表中移除一项*/
    this.removeAt = function(position){ if(position > -1 && position < this.length){ var current = this.head, previous, index = 0; if(position === 0){ this.head = current.next; }else{ while(index++ < position){ previous = current; current = current.next; } previous.next = current.next; } this.length--; return current.element; }else{ return null; } };
    /*向链表特定位置插入一个新的项*/
    this.insert = function(position, ele){ if(position >= 0 && position <= this.length){ var node = new Node(ele) var current = this.head, previous, index = 0; if(position === 0){ node.next = current; this.head = node; }else{ while(index++ < position){ node.next = current; current = current.next; } node.next = current; previous.next = node; } this.length++; return true; }else{ return false; } };
    /*由于列表使用了Node类,就需要重写继承子javascript对象默认的toString方法,让其只输出元素的值*/
    this.toString = function(){ var current = this.head, string = ''; while(current){ if(current.element == this.head.element){ string += current.element; }else{ string += ',' + current.element; } current = current.next; } return string.slice(0) };
    /*返回元素在链表中的索引,如果没有就返回-1*/
    this.indexOf = function(ele){ var current = this.head, index = 0; while(current){ if(ele === current.element){ return index; } index++; current = current.next; } return -1; };
    /*移除链表中的一项*/
    this.remove = function(ele){ var index = this.indexOf(ele); return this.removeAt(index) };
    /*判断链表是否为空,为空返回true,不为空返回false*/
    this.isEmpty = function(){ return this.length ===0; };
    /*返回链表所包含的元素的个数*/
    this.size = function(){ return this.length; };
      /*返回整个链表的结构-->代码结构*/
    this.getHead = function(){ return this.head; } }
  • 相关阅读:
    一起学ORBSLAM2(10)ORBSLAM中的相似变换矩阵的计算
    一起学ORBSLAM2(9)ORBSLAM的PNP解决方案
    一起学ORBSLAM2(8)ORBSLAM的loopclosing
    一起学ORBSLAM2(7)ORBSLAM中的优化问题
    一起学ORBSLAM2(6)ORBSLAM中的特征匹配
    IntelliJ IDEA 2018.2.2远程调试Tomcat的配置方法
    window.location.href 跳转页面时传递参数并且在新页面接收参数
    form.loadRecord(record)后isDirty总是true的解决办法
    ExtJS让被遮盖的窗体显示在最前面以及解决Ext.MessageBox提示框被TabPanel覆盖的方法【转】
    extjs store中数据转换成json
  • 原文地址:https://www.cnblogs.com/guojikun/p/6204098.html
Copyright © 2020-2023  润新知