If you are learning data structures, a linked list is one data structure you should know. If you do not really understand it or how it is implemented in JavaScript, this article is here to help you.

如果您正在学习数据结构,则链表是您应该知道的一种数据结构。 如果您不太了解它或如何在JavaScript中实现它,本文将为您提供帮助。

In this article, we will discuss what a linked list is, how it is different from an array, and how to implement it in JavaScript. Let's get started.

在本文中,我们将讨论什么是链表,链表与数组的不同之处以及如何在JavaScript中实现。 让我们开始吧。

什么是链表? (What is a Linked List?)

A linked list is a linear data structure similar to an array. However, unlike arrays, elements are not stored in a particular memory location or index. Rather each element is a separate object that contains a pointer or a link to the next object in that list.

链表是类似于数组的线性数据结构。 但是,与数组不同,元素不存储在特定的存储位置或索引中。 而是每个元素都是一个单独的对象,其中包含一个指针或指向该列表中下一个对象的链接。

Each element (commonly called nodes) contains two items: the data stored and a link to the next node. The data can be any valid data type. You can see this illustrated in the diagram below.

每个元素(通常称为节点)包含两项:存储的数据和到下一个节点的链接。 数据可以是任何有效的数据类型。 您可以在下图中看到这一点。

The entry point to a linked list is called the head. The head is a reference to the first node in the linked list. The last node on the list points to null. If a list is empty, the head is a null reference.

链接列表的入口称为头。 头是对链表中第一个节点的引用。 列表上的最后一个节点指向null。 如果列表为空,则标头为空引用。

In JavaScript, a linked list looks like this:

在JavaScript中,链接列表如下所示:

const list = {head: {value: 6next: {value: 10                                             next: {value: 12next: {value: 3next: null  }}}}}
};

链表的优点 (An advantage of Linked Lists)

  • Nodes can easily be removed or added from a linked list without reorganizing the entire data structure. This is one advantage it has over arrays.可以轻松地从链接列表中删除或添加节点,而无需重新组织整个数据结构。 这是它比数组具有的优势之一。

链表的缺点 (Disadvantages of Linked Lists)

  • Search operations are slow in linked lists. Unlike arrays, random access of data elements is not allowed. Nodes are accessed sequentially starting from the first node.链接列表中的搜索操作很慢。 与数组不同,不允许随机访问数据元素。 从第一个节点开始依次访问节点。
  • It uses more memory than arrays because of the storage of the pointers.由于指针的存储,它比数组使用更多的内存。

链接列表的类型 (Types of Linked Lists)

There are three types of linked lists:

链接列表有三种类型:

  • Singly Linked Lists: Each node contains only one pointer to the next node. This is what we have been talking about so far.

    单链接列表 :每个节点仅包含一个指向下一个节点的指针。 到目前为止,这就是我们一直在谈论的内容。

  • Doubly Linked Lists: Each node contains two pointers, a pointer to the next node and a pointer to the previous node.

    双链表 :每个节点包含两个指针,一个指向下一个节点的指针和一个指向上一个节点的指针。

  • Circular Linked Lists: Circular linked lists are a variation of a linked list in which the last node points to the first node or any other node before it, thereby forming a loop.

    循环链表 :循环链表是链表的一种变体,其中最后一个节点指向第一个节点或它之前的任何其他节点,从而形成一个循环。

在JavaScript中实现列表节点 (Implementing a List Node in JavaScript)

As stated earlier, a list node contains two items: the data and the pointer to the next node. We can implement a list node in JavaScript as follows:

如前所述,列表节点包含两项:数据和指向下一个节点的指针。 我们可以在JavaScript中实现列表节点,如下所示:

class ListNode {constructor(data) {this.data = datathis.next = null                }
}

在JavaScript中实现链接列表 (Implementing a Linked List in JavaScript)

The code below shows the implementation of a linked list class with a constructor. Notice that if the head node is not passed, the head is initialised to null.

下面的代码显示了使用构造函数的链表类的实现。 请注意,如果未传递头节点,则头将初始化为null。

class LinkedList {constructor(head = null) {this.head = head}
}

全部放在一起 (Putting it all together)

Let's create a linked list with the class we just created. First, we create two list nodes, node1 and node2 and a pointer from node 1 to node 2.

让我们用刚刚创建的类创建一个链表。 首先,我们创建了两个列表中的节点, node1node2和节点1到节点2的指针。

let node1 = new ListNode(2)
let node2 = new ListNode(5)
node1.next = node2

Next, we'll create a Linked list with the node1.

接下来,我们将使用node1创建一个链接列表。

let list = new LinkedList(node1)

Let's try to access the nodes in the list we just created.

让我们尝试访问刚刚创建的列表中的节点。

console.log(list.head.next.data) //returns 5

一些LinkedList方法 (Some LinkedList methods)

Next up, we will implement four helper methods for the linked list. They are:

接下来,我们将为链接列表实现四种帮助方法。 他们是:

  1. size()尺寸()
  2. clear()明确()
  3. getLast()getLast()
  4. getFirst()getFirst()

1. size() (1. size())

This method returns the number of nodes present in the linked list.

此方法返回链表中存在的节点数。

size() {let count = 0; let node = this.head;while (node) {count++;node = node.next}return count;
}

2. clear() (2. clear())

This method empties out the list.

此方法清空列表。

clear() {this.head = null;
}

3. getLast() (3. getLast())

This method returns the last node of the linked list.

此方法返回链表的最后一个节点。

getLast() {let lastNode = this.head;if (lastNode) {while (lastNode.next) {lastNode = lastNode.next}}return lastNode
}

4. getFirst() (4. getFirst())

This method returns the first node of the linked list.

此方法返回链表的第一个节点。

getFirst() {return this.head;
}

摘要 (Summary)

In this article, we discussed what a linked list is and how it can be implemented in JavaScript. We also discussed the different types of linked lists as well as their overall advantages and disadvantages.

在本文中,我们讨论了什么是链表以及如何在JavaScript中实现链表。 我们还讨论了链表的不同类型及其整体优缺点。

I hope you enjoyed reading it.

希望您喜欢阅读。

Want to get notified when I publish a new article? Click here.

想要在我发表新文章时得到通知吗? 请点击这里 。

翻译自: https://www.freecodecamp.org/news/implementing-a-linked-list-in-javascript/

如何在JavaScript中实现链接列表相关推荐

  1. 如何在JavaScript中验证电子邮件地址

    如何在JavaScript中验证电子邮件地址? #1楼 与squirtle相比 ,这是一个复杂的解决方案,但是在正确验证电子邮件方面做得非常出色: function isEmail(email) { ...

  2. 如何在 JavaScript 中获取当前日期?

    问: 想要改进这篇文章?提供这个问题的详细答案,包括引文和解释为什么你的答案是正确的.没有足够细节的答案可能会被编辑或删除. 如何在 JavaScript 中获取当前日期? 答1: HuntsBot周 ...

  3. regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾

    regexp 好汉字符串 by Catherine Vassant (aka Codingk8) 由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8) 如何在JavaScrip ...

  4. 如何在R中正确使用列表?

    本文翻译自:How to Correctly Use Lists in R? Brief background: Many (most?) contemporary programming langu ...

  5. 如何在JavaScript中实现堆栈和队列?

    本文翻译自:How do you implement a Stack and a Queue in JavaScript? What is the best way to implement a St ...

  6. 如何在javascript中使用多个分隔符分割字符串?

    如何在JavaScript中使用多个分隔符拆分字符串? 我正在尝试在逗号和空格上进行拆分,但是AFAIK,JS的拆分功能仅支持一个分隔符. #1楼 对于那些想要在拆分功能中进行更多自定义的人,我编写了 ...

  7. 现在JavaScript日期–如何在JavaScript中获取当前日期

    Many applications you build will have some sort of a date component, whether it's the creation date ...

  8. python字符串筛选输出_如何在Python中过滤字符串列表

    Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...

  9. 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法

    by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法 (How to use ...

最新文章

  1. mybatis plus 插入生成id_springcloud微服务快速教程之分布式ID解决方案(mybatisplus篇)...
  2. nlopt 二次优化
  3. Xilinx IP核之FIFO
  4. 【快乐水题】506. 相对名次
  5. sap 一代增强_在SAP标准实施中不起眼的“小”功能,居然融了3个亿
  6. 计算机秋招必备!广州互联网大厂企业整理清单!
  7. 2019-02-27-算法-进化(寻找两个有序数组的中位数)
  8. python字典内置方法_柳小白Python学习笔记 12 内置方法之字典方法
  9. 数据分析中常用的数据模型
  10. AirPlay 投影到 Mac 看不到选项如何解决?
  11. mysql临时表多线程时能用吗_学会使用临时表优化,切记不要乱用临时表(记录一)...
  12. 微信撤回软件安卓版_微信强制撤回软件下载-微信强制撤回消息工具(不限时间) v1.0安卓版_5577安卓网...
  13. 谷歌浏览器开启深色模式
  14. 对角化求可逆矩阵_矩阵对角化方法
  15. (一)事务与并发控制
  16. 【杂谈】嵌入式软件数据结构的特点
  17. 人脸对齐SDM原理----Supervised Descent Method and its Applications to Face Alignment
  18. Java学习:从入门到精通week4
  19. oracle dbms是什么意思,什么是Oracle特殊包和DBMS?
  20. android 英汉字典,英汉全文字典安卓版

热门文章

  1. JavaScript判断对象是否为空对象或空数组
  2. 实现对学生信息的增加操作
  3. rust 官服指令_RUST 命令大全(包括服务器指令)
  4. .net里鼠标选中的text数据怎么获取_Python数据科学实践 | 爬虫1
  5. python3 列表转字节_Python 3.9!10大新特性值得关注
  6. iOS 9 通用链接(Universal Links)
  7. Swift 值类型和引用类型的内存管理
  8. F#探险之旅(三):命令式编程(上)
  9. tomcat环境部署
  10. mapreduce作业reduce被大量kill掉