React 前端导航

js数据结构与算法之链表

介绍

要存储多个元素,一般都会选择数组,但是这种数据结构有一个缺点:一般数组的大小都是固定的,从数组的起点或中间插入或移除元素成本有点高。这时候就可以选择链表。

链表存储有序的元素集合,但不同于数组,链表的元素在内存中不是连续放置的,每个元素由一个存储元素本身的节点个指向下一个元素的引用组成。

相对于传统的数组,链表的一个好处在于,添加或移除元素的时候不需要移动其他元素。然 而,链表需要使用指针,因此实现链表时需要额外注意。数组的另一个细节是可以直接访问任何 位置的任何元素,而要想访问链表中间的一个元素,需要从起点(表头)开始迭代列表直到找到所需的元素。

 

 

创建链表

function LinkedList() { 
 let Node = function(element){ // Node辅助类,要加入列表的项。element要加入的值,next表示指向列表下一个节点 
         this.element = element; 
         this.next = null; 
 }; 
 let length = 0; // 存储列表项数量
 let head = null; // 存储第一个节点的引用,存到变量head中。 
 this.append = function(element){}; //向列表项尾部添加新的项
 this.insert = function(position, element){}; //向列表指定位置插入新的项
 this.removeAt = function(position){}; //从列表特定位置移除一项
 this.remove = function(element){}; //从列表中移除一项
 this.indexOf = function(element){}; //返回元素在列表的索引,没有则返回-1
 this.isEmpty = function() {}; //如果链表中不包含任何元素,返回true,如果链表长度大于0,则返回false
 this.size = function() {}; //返回链表的长度
 this.getHead = function(){}; 
 this.toString = function(){}; //输入链表元素值
 this.print = function(){}; 
}

 

向链表尾部添加元素

两种场景:列表为空,添加的是第一个元素;列表不为空,向其追加元素

this.append = function (element) {
    let node = new Node(element), //传入element值,作为Node项 
        current; //{2} 
    if (head === null) { //列表中第一个节点为空时,此时head指向node
        head = node;
    } else {
        current = head;
        //循环列表,直到找到最后一项
        while (current.next) {
            current = current.next;
        }
        //找到最后一项,将其next赋为node,建立链接
        current.next = node;
    }
    length++; //更新列表的长度
};

 

从链表中移除元素

两种场景:移除第一个元素;移除第一个以外的元素。

this.removeAt = function (position) {
    //检查越界值
    if (position > -1 && position < length) {
        let current = head,
            previous,
            index = 0;
        //移除第一项
        if (position === 0) {
            head = current.next;
        } else {
            while (index++ < position) {
                previous = current;
                current = current.next;
            }
            //将previous与current的下一项链接起来:跳过current,从而移除它
            previous.next = current.next;
        }
        length--;
        return current.element;
    } else {
        return null;
    }
};

 

在任意位置插入元素

this.insert = function (position, element) {
    //检查越界值
    if (position >= 0 && position <= length) { 
        let node = new Node(element),
            current = head,
            previous,
            index = 0;
        if (position === 0) { //在第一个位置添加
            node.next = current;  
            head = node;
        } else {
            while (index++ < position) {
                previous = current;
                current = current.next;
            }
            node.next = current; 
            previous.next = node;
        }
        length++; //更新列表的长度
        return true;
    } else {
        return false; 
    }
};

 

toString方法

this.toString = function () {
    let current = head,
        string = '';  
    while (current) {
        string += current.element + (current.next ? 'n' : ''); 
        current = current.next; 
    }
    return string;
};

 

indexOf方法

this.indexOf = function (element) {
    let current = head,
        index = -1;
    while (current) {
        if (element === current.element) {
            return index; 
        }
        index++; 
        current = current.next; 
    }
    return -1;
};

 

isEmpoty方法

this.isEmpty = function() { 
        return length === 0; 
};

 

size方法

this.size = function() { 
        return length; 
};

 

getHead方法

this.getHead = function(){ 
        return head; 
};

声明:本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。邮箱:farmerlzj@163.com。 本站原创内容未经允许不得转载,或转载时需注明出处: 内容转载自: React前端网:https://qianduan.shop/blogs/detail/165
想做或者在做副业的朋友欢迎加微信交流:farmerlzj,公众号:生财空间站。

#js#链表

相关推荐

原型与原型链、继承

原型与原型链、继承简单实现

浏览器中的js事件循环(Event loop)

本文将简述浏览器中的js事件循环机制,帮助我们理解浏览器环境js代码是如何运行的。Javascript的一大特点是单线程,也就意味着同一时间他只能做一件事。事件循环(Event Loop)是为了协调事件,用户交互,UI渲染,网络处理等行为,防止线程阻塞而诞生的。