数据结构教程网盘链接_数据结构101:链接列表

数据结构教程网盘链接

by Kevin Turney

凯文·特尼(Kevin Turney)

Like stacks and queues, Linked Lists are a form of a sequential collection. It does not have to be in order. A Linked list is made up of independent nodes that may contain any type of data. Each node has a reference to the next node in the link.

像堆栈和队列一样 ,链接列表是顺序集合的一种形式。 它不一定是有序的。 链接列表由可能包含任何类型的数据的独立节点组成。 每个节点都有对链接中下一个节点的引用。

We can emulate stacks and queues with linked lists. We can as well use it as a base to create or augment other data structures. With linked lists, our primary concerns are with fast insertions and deletions, which are more performant over arrays.

我们可以使用链接列表来模拟堆栈和队列。 我们也可以将其用作创建或扩充其他数据结构的基础。 对于链表,我们的主要关注点是快速插入和删除,它们在数组上的性能更高。

The building block of this structure is a Node.

此结构的构建块是节点。

const Node = function(value) {  this.value = value;  this.next = null;};

Our Node is built with two properties, a value to hold data, and next, a reference initially set to null. The next property is used to “point” to the next Node in the linking. One of the disadvantages of linked lists is that each reference requires a larger memory overhead than an array.

我们的Node具有两个属性,一个用于保存数据的valuenext是最初设置为null的引用。 next属性用于“指向”链接中的下一个节点。 链表的缺点之一是每个引用比数组需要更大的内存开销。

实作 (Implementation)

const LinkedList = function(headvalue) {  // !! coerces a value to a Boolean  if (!!headvalue) {    return "Must provide an initial value for the first node"  } else {    this._head = new Node(headvalue);    this._tail = this.head;  }};

In our second constructor, we test for a value to provide for the first Node. If true, we proceed to create a new Node with the value passed and set the head to tail initially.

在第二个构造函数中,我们测试要提供给第一个Node的值。 如果为true,我们将继续使用传递的值创建一个新的Node,并将head最初设置为tail。

插入 (Insertion)

LinkedList.prototype.insertAfter = function(node, value) {  let newNode = new Node(value);  let oldNext = node.next;  newNode.next = oldNext;  node.next = newNode;  if (this._tail === node) {    this._tail = newNode;  }  return newNode;};

For this method, we create a new Node and adjust the references. The former next reference of the original node is now directed to newNode. The newNode’s next reference is “pointed” to what the previous node’s next was referring to. Finally, we check and reset the tail property.

对于此方法,我们创建一个新的Node并调整引用。 现在将原始节点的前一个下一个引用定向到newNode。 newNode的下一个引用“指向”上一个节点的下一个引用。 最后,我们检查并重置tail属性。

LinkedList.prototype.insertHead = function(value) {  let newHead = new Node(value);  let oldHead = this._head  newHead.next = oldHead;  this._head = newHead;  return this._head;};
LinkedList.prototype.appendToTail = function(value) {  let newTail = new Node(value);  this._tail.next = newTail;  this._tail = newTail;  return this._tail;};

Insertion at the beginning or end of a linked list is fast, operating in constant time. For this, we create a new node with a value and rearrange our reference variables. We reset the node which is now the head with insertHead or the tail with appendToTail.

快速插入链表的开头或结尾,并且操作时间固定。 为此,我们创建一个具有值的新节点,并重新排列参考变量。 我们将节点重置为现在的头,其头为insertHead或尾部为appendToTail

These operations represent fast insertions for collections, push for stacks, and enqueue for queues. It may come to mind that unshift for arrays is the same. No, because with unshift all members of the collection must be moved one index over. This makes it a linear time operation.

这些操作表示对集合的快速插入,对堆栈的推送以及对队列的排队。 可能会想到,数组的不变移位是相同的。 不可以,因为在取消移位时,必须将集合的所有成员移到一个索引上。 这使其成为线性时间操作。

删除中 (Deletion)

LinkedList.prototype.removeAfter = function(node) {  let removedNode = node.next;  if (!!removedNode) {    return "Nothing to remove"  } else {    let newNext = removedNode.next    node.next = newNext;    removedNode.next = null; // dereference to null to free up memory    if (this._tail === removedNode) {      this._tail = node;    }  }  return removedNode;};

Starting with a test for a node to remove, we proceed to adjust the references. Dereferencing the removedNode and setting it to null is important. This frees up memory and avoids having multiple references to the same object.

从测试要删除的节点开始,我们继续调整引用。 removedNode引用removedNode并将其设置为null很重要。 这样可以释放内存,并避免对同一对象有多个引用。

LinkedList.prototype.removeHead = function() {  let oldHead = this._head;  let newHead = this._head.next;  this._head = newHead;  oldHead.next = null;  return this._head;};

Deletion of a head and of a specified node in, removeAfter, are constant time removals. In addition, if the value of the tail is known, then tail removal can be done in O(1). Else we have to move linearly to the end to remove it, O(N);

删除头和指定节点中的removeAfter是恒定时间删除。 另外,如果知道尾巴的值,则可以在O(1)中进行尾巴去除。 否则,我们必须线性移动到最后才能将其删除,O(N);

循环播放 (Looping and forEach)

We use the following to iterate through a linked list or to operate on each node value.

我们使用以下内容迭代链接列表或对每个节点值进行操作。

LinkedList.prototype.findNode = function(value) {  let node = this._head;  while(node) {    if (node.value === value) {      return node;    }    node = node.next;  }  return `No node with ${value} found`;};
LinkedList.prototype.forEach = function(callback) {  let node = this._head;  while(node) {    callback(node.value);    node = node.next;  }};
LinkedList.prototype.print = function() {  let results = [];  this.forEach(function(value) {    result.push(value);  });  return result.join(', ');};

The main advantage of Linked Lists is fast insertions and deletions without rearranging items or reallocation of space. When we use an array, the memory space is contiguous, meaning we keep it all together. With linked lists, we can have memory spaces all over the place, non-contiguous storage through the use of references. For arrays, that locality of references means that arrays have better caching of values for faster lookup. With linked lists, caching is not optimized and access time takes longer.

链接列表的主要优点是快速插入和删除,而无需重新排列项目或重新分配空间。 当我们使用数组时,内存空间是连续的,这意味着我们将它们保持在一起。 使用链表,我们可以在各处拥有存储空间,通过使用引用可以实现非连续存储。 对于数组,引用的局部性意味着数组可以更好地缓存值以加快查找速度。 使用链接列表时,无法优化缓存,并且访问时间会更长。

Another aspect of linked lists is different types of configuration. Two primary examples are circularly linked, where the tail has a reference to the head and the head to the tail. Doubly linked is when, in addition to the node having a reference to the next node, also has a reference looking back to the previous node.

链表的另一方面是不同类型的配置。 两个主要的示例是循环链接的,其中,尾部引用了头部,头部引用了尾部。 链接是指除了该节点具有对下一个节点的引用之外,还具有回溯到前一个节点的引用。

时间复杂度 (Time Complexity)

Insertion

插入

  • insertHead, appendToTail — O(1)

    insertHead,appendToTail — O(1)
  • if a specific node is known, insertAfter — O(1)

    如果已知特定节点,则insertAfter — O(1)

Deletion

删除中

  • removeHead — O(1);

    removeHead — O(1);
  • if a specific node is known, removeAfter — O(1)

    如果已知特定节点,则removeAfter — O(1)
  • if the node is not known — O(N)

    如果节点未知— O(N)

Traversing

遍历

  • findNode, forEach, print — O(N)

    findNode,forEach,打印— O(N)

翻译自: https://www.freecodecamp.org/news/data-structures-101-linked-lists-254c82cf5883/

数据结构教程网盘链接

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393330.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

多线程之间的通信(等待唤醒机制、Lock 及其它线程的方法)

一、多线程之间的通信。 就是多个线程在操作同一份数据, 但是操作的方法不同。     如: 对于同一个存储块,其中有两个存储位:name sex, 现有两个线程,一个向其中存放数据,一个打印其中的数…

Linux iptables 配置详解

一、配置一个filter表的防火墙 1. 查看本机关于 iptables 的设置情况 # iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) …

06 Nginx

1.检查linux上是否通过yum安装了nginx rpm -qi nginx2.解决安装nginx所依赖包 yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel ope…

java编写安卓程序代码,安卓:从Android的Java源代码code创建UML

i am looking for a program that can create automatically an Uml from my Java-Android source code.I have tested ArgoUml, but it does not support Android.Have any one a suggestion?Thanks!解决方案I can second what Tom Morris wrote in the comment above. Even …

leetcode1052. 爱生气的书店老板(滑动窗口)

今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。 在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气&#xf…

amazon alexa_在Amazon Alexa上推出freeCodeCamp编码琐事测验

amazon alexaNow you can learn coding concepts hands-free using an Amazon Echo.现在,您可以使用Amazon Echo免提学习编码概念。 freeCodeCamp.org contributor David Jolliffe created a quiz game with questions on JavaScript, CSS, networking, and comput…

第一类第二类丢失更新

第一类丢失更新 A事务撤销时,把已经提交的B事务的更新数据覆盖了。这种错误可能造成很严重的问题,通过下面的账户取款转账就可以看出来: 时间 取款事务A 转账事务B T1 开始事务 T2 开始事务 T3 查询账户余额为1000元 …

oracle数据字典表与视图

oracle数据字典表与视图 数据字典是数据的数据,也就是元数据。描述了数据库的物理与逻辑存储与相应的信息。模式中对象的定义信息,安全信息,完整性约束信息,和部分的性能监控信息等。数据字典表 与视图存储在system表空间中的。有…

团队作业——项目Alpha版本发布

---恢复内容开始--- https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/3329 <作业要求的链接> Gorious Computer <写上团队名称> 发布项目α版本&#xff0c;对项目…

java脏字过滤_脏字过滤

1.[文件]SensitiveWordFilter.java ~ 7KB下载(141)package com.forgov.sharpc.infrastruture.util;import static java.util.Collections.sort;import java.util.ArrayList;import java.util.Collection;import java.util.Comparator;import java.util.HashSet;import java.uti…

react中使用构建缓存_完整的React课程:如何使用React构建聊天室应用

react中使用构建缓存In this video course, youll learn React by building a chat room app.在本视频课程中&#xff0c;您将通过构建聊天室应用程序来学习React。 By the end of the video, youll have a solid understanding of React.js and have your very own chat room…

leetcode1509. 三次操作后最大值与最小值的最小差

给你一个数组 nums &#xff0c;每次操作你可以选择 nums 中的任意一个元素并将它改成任意值。 请你返回三次操作后&#xff0c; nums 中最大值与最小值的差的最小值。 示例 1&#xff1a; 输入&#xff1a;nums [5,3,2,4] 输出&#xff1a;0 解释&#xff1a;将数组 [5,3,…

MySQL异步复制

准备&#xff1a;主备库版本一致&#xff0c;正常安装软件。 1、主库上设置一个复制使用的账户&#xff1a; mysql> grant replication slave on *.* to rep1192.168.100.136 identified by dbking; Query OK, 0 rows affected (0.18 sec) mysql> select user,host,passw…

开源一个爬取redmine数据的测试报告系统

背景 软件测试的最后有一道比较繁琐的工作&#xff0c;就是编写测试报告。手写测试报告在数据统计和分析上面要耗费比较大的事件和精力。之前工作室使用mantis管理bug缺陷。公司有内部有个系统&#xff0c;可以直接从mantis上面获取数据并进行统计&#xff0c;生成一份测试报告…

java cxf 双向通讯_CXF 在spring boot 2 发布多个服务

0. 问题来源之前配置cxf服务端都是在spring 3以下&#xff0c;后来使用spring mvc 还都是基于xml的配置文件模式&#xff0c;在springboot模式下&#xff0c;实现起来更为简单了。此次记录下spring boot 2下的实现方式。1. 准备工作项目中&#xff0c;直接拉入spring boot cxf相…

小程序 坚屏_如何构建坚如磐石的应用程序

小程序 坚屏不同的应用程序设计选项概述 (An overview of different app design options) When we design software, we constantly think about error cases. Errors have a huge impact on the way we design and architecture a solution. So much so, in fact, that there …

C# 分层

三层架构分为&#xff1a;表现层&#xff08;UI&#xff09;、业务逻辑层&#xff08;BLL&#xff09;、数据访问层&#xff08;DAL&#xff09;再加上实体类库&#xff08;Model&#xff09; 转载请注明出自朱朱家园http://blog.csdn.net/zhgl7688 1、实体类库&#xff08;Mod…

leetcode1177. 构建回文串检测(前缀和)

给你一个字符串 s&#xff0c;请你对 s 的子串进行检测。 每次检测&#xff0c;待检子串都可以表示为 queries[i] [left, right, k]。我们可以 重新排列 子串 s[left], …, s[right]&#xff0c;并从中选择 最多 k 项替换成任何小写英文字母。 如果在上述检测过程中&#xf…

java界面化二叉排序树_层次序创建二叉树(图形界面和控制台输入实现)

1 2018.11.72 XT34 /**5 * 功能&#xff1a;构造二叉树6 * 说明&#xff1a;7 * 1.主函数输入模式有两种&#xff0c;BT参数 true 图形界面&#xff0c;false 控制台输入8 * 2.构造树是按层次遍历结果输入的 如&#xff1a;ABCDE*F**GH9 */1011 import javax.swing.*;12 import…

web开发环境_Web开发人员的开发环境

web开发环境With all the tools and programs available, it can be challenging to figure out the best way to set up your development environment on your computer.使用所有可用的工具和程序&#xff0c;寻找在计算机上设置开发环境的最佳方法可能是一项挑战。 In this…