数据结构栈和队列

When you want to store several elements somewhere in a program, the go-to data type is an array. But there are a few disadvantages:

当您要将几个元素存储在程序中的某个位置时,go-to数据类型是一个数组。 但是有一些缺点:

  • Elements must be stored in a certain order, and belong to indexes. This takes up space to keep track of.元素必须以一定顺序存储,并且属于索引。 这占用了要跟踪的空间。
  • If we want to delete an element from the array, we need to shift all the elements after it one index forward.如果要从数组中删除元素,则需要将其后的所有元素向前移动一个索引。
  • If we want to add an element to the array, we need to shift all the elements after it one index backward.如果要向数组添加元素,则需要向后移动所有元素一个索引。

Linked lists are an alternative to arrays. Instead of storing the list elements in one contiguous location being bounded by the array structure, elements in linked lists are represents as nodes. Each node contains two values, a value and a ‘next pointer’ that links to the next node object. The head has no next pointer linking to it, and the tail has no next pointer (a None next pointer).

链接列表是数组的替代方法。 而不是将列表元素存储在受数组结构限制的一个连续位置中,而是将链接列表中的元素表示为节点。 每个节点包含两个值,一个值和一个链接到下一个节点对象的“下一个指针”。 头没有链接到它的下一个指针,而尾没有任何下一个指针( None下一个指针)。

One advantage of using linked lists is that the sequence of elements does not matter, since each’s next pointer links to the following element, regardless of the order they are placed in. This obviously has many benefits in terms of list operations and memory usage.

使用链接列表的一个优点是元素的顺序无关紧要,因为每个元素的下一个指针都链接到下一个元素,而与它们的放置顺序无关。这显然在列表操作和内存使用方面有很多好处。

Note that even though linked lists do not have order of elements, they will be visualized as such to be more understandable.

请注意,即使链表没有元素的顺序,它们也将可视化,以便于理解。

This means that if we want to insert an element at an index, we don’t actually have to insert it at that location in some sequential list. One can just add the element at an arbitrary location and rework the linkages.

这意味着,如果我们想在索引处插入元素,则实际上不必在顺序表中的该位置插入元素。 只需将元素添加到任意位置,然后重新进行链接即可。

To add a node between nodes i and i+1, where i represents the index of the node in the sequence of the linked list (for the 4th node, i = 3):

要在节点ii + 1之间添加一个节点,其中i表示该节点在链表的序列中的索引(对于第4个节点, i = 3):

  • Create a new node with the desired value. Store it at an arbitrary location.创建具有所需值的新节点。 将其存储在任意位置。
  • Set node i’s next pointer to the new node. Since each node can only have one next pointer, this ‘deletes’ the original next pointer to node i+1.

    将节点i的下一个指针设置为新节点。 由于每个节点只能有一个下一个指针,因此这会“删除”到节点i + 1的原始下一个指针。

  • Set the new node’s next pointer to node i+1.

    设置新节点的下一个指针指向节点i +1。

To delete the ith node:

要删除第i个节点:

  • Set the i-1th node’s next pointer equal to the i+1th node. This has the effect of ‘skipping’ the deleted node.

    将第i- 1个节点的下一个指针设置为等于第i +1个节点。 这具有“跳过”已删除节点的效果。

  • Although it’s not necessary, since the linkages have been changed, the node can be physically deleted from memory.尽管没有必要,但是由于链接已更改,因此可以从内存中物理删除该节点。

Note how much more efficient this is than index-based arrays, in which inserting or deleting an element at index i affects all the elements before it.

请注意,这比基于索引的数组要有效得多,在基于索引的数组中,在索引i处插入或删除元素会影响之前的所有元素。

However, traversing a singly linked list — the one demonstrated above, with only one next pointer — is inefficient in that we can only move in one direction. If we want to access to fourth element, we must begin at the head and travel down each element until the fourth element is reached. Afterwards, if we want the second element, we must start from the beginning again.

但是,遍历一个单链列表(上面演示的一个列表,只有一个下一个指针)效率低下,因为我们只能沿一个方向移动。 如果要访问第四个元素,则必须从头开始并向下移动每个元素,直到到达第四个元素。 此后,如果我们需要第二个元素,则必须从头开始。

The doubly linked list provide more freedom in the traversing by providing both a previous and a next pointer. Hence, to access the third element after accessing the fourth element, one can just follow the previous pointer instead of needing to reset at the head and follow the chain to the third element.

双链表通过提供前一个和下一个指针,在遍历中提供了更大的自由度。 因此,要在访问第四个元素之后访问第三个元素,就可以跟随前一个指针,而无需在头部重新设置并遵循到第三个元素的链。

Inserting and deleting is also faster with doubly linked lists. In singly linked lists, the previous and next nodes after a specified node are required for both insertion and deletion. In order to access the previous node, the linked list must be traversed from the start. In doubly linked lists, this can simply be accessed through the ‘previous’ pointer.

使用双向链接列表,插入和删除也更快。 在单链列表中,插入和删除都需要指定节点之后的上一个和下一个节点。 为了访问上一个节点,必须从头开始遍历链表。 在双向链接列表中,可以简单地通过“上一个”指针进行访问。

Circular linked lists are linked lists that repeat themselves because the tail node links back to the head node. Because of this, there are technically no tail and head nodes in a circular linked list.

循环链表是重复的链表,因为尾节点链接回头节点。 因此,在循环链表中技术上没有尾部和头节点。

These types of lists can be very helpful for the implementation of queue data structures and other efficient applications. For instance, if I have the following queue (a list of fixed length, in which two operations, enqueueing and dequeuing, add items to the end of the queue and remove items from the front of the queue, respectively — like a store checkout queue):

这些类型的列表对于实现队列数据结构和其他有效的应用程序非常有帮助。 例如,如果我有以下队列(固定长度的列表,其中有两个操作,使入队和出队)分别添加到队列的末尾和从队列的最前面移去—例如商店结帐队列):

If I wanted to remove the first element, I could just set it to None, but there is no reason to spend lots of memory and power to move the other two elements, for reasons you’ll see soon.

如果要删除第一个元素,可以将其设置为“无”,但是没有理由花很多内存和精力来移动其他两个元素,因为您很快就会看到。

Say I decide to enqueue three more items: if this were a regular list, the queue would either be full, the list would be programmed (very inefficiently) to move the list such that there are no None objects at the front, or there would need to be some sort of incredibly complex function using modulo that converts a list into some sort of circular one.

假设我决定再加入三个项目:如果这是一个常规列表,则队列要么已满,要么对列表进行了编程(效率非常低下)以移动列表,从而使得前面没有None对象,或者需要使用模运算将列表转换为某种循环的模数,这是某种极其复杂的函数。

With circular linked lists, this problem is nonexistent, since they have not only no order but also no index.

对于循环链表,不存在此问题,因为它们不仅没有顺序而且没有索引。

For instance, when multiple applications run on your PC, the operating system will put each of the running applications on a queue and cycle through them rapidly, giving each a slice of time to execute, then putting them on pause while the CPU is dedicated to another application. Since these lists are looped through at lightning speeds, it’s important that a list be circular.

例如,当您的PC上运行多个应用程序时,操作系统会将每个正在运行的应用程序置于队列中并快速循环通过它们,给每个应用程序分配一小段时间来执行,然后在CPU专用于另一个应用程序。 由于这些列表以闪电般的速度循环通过,因此列表必须是圆形的,这一点很重要。

Moreover, circular linked lists are exceptionally easy to create — just link the tail node to the head node in a non-circular linked list. They have countless applications in creating efficient queues that can handle massive sizes.

此外,圆形链接列表非常容易创建-只需在非圆形链接列表中将尾节点链接到头节点即可。 他们在创建可以处理大量数据的高效队列中拥有无数的应用程序。

In comparison between arrays and linked lists:

比较数组和链表:

  • Although linked lists can perform list operations extraordinarily quickly, in order to access an element, it must traverse through at least part of the linked list (O(n) time to access), whereas arrays can just reference an index. This is part of the cost of not being ordered.

    尽管链表可以非常快速地执行列表操作,但是为了访问元素,它必须遍历链表的至少一部分(访问时间为O( n )时间),而数组只能引用索引。 这是未定购成本的一部分。

  • Linked lists are generally more efficient, although they are best suited for use cases that a) make many operations and b) do not require the extraction of element values often.链接列表通常更有效,尽管它们最适合以下情况:a)进行许多操作并且b)不需要经常提取元素值。
  • In order to store pointers (next or previous and next), linked lists take up a bit more memory than arrays (balancing speed vs space).为了存储指针(下一个,上一个和下一个和下一个),链接列表占用的内存比数组多(平衡速度与空间)。
  • Arrays have better cache locality than linked lists.数组比链接列表具有更好的缓存位置。
  • Random access in linked lists is not allowed, and the access of any element must begin at the head. Hence, performing standard and well-developed search procedures like binary search do not work in linked lists, although there are search methods designed especially for linked lists.不允许在链表中进行随机访问,并且任何元素的访问都必须从头开始。 因此,尽管存在一些专门为链接列表设计的搜索方法,但是执行标准和完善的搜索程序(如二进制搜索)在链接列表中不起作用。

Generally, linked lists are implemented with two classes. Pseudocode:

通常,链表是用两个类实现的。 伪代码:

class Node (value, next=None) #none by default    self.value = value #this is the value the node holds    self.next = next #this will be another node objectclass LinkedList (head)    self.head = head    function add_at_index(): ...

Each node has a value and is linked to the next (assuming a singly linked list), and the only variable that is important to the linked list is the node at the start, because it can traverse from that node until it reaches another desired node to perform functions like adding at certain indices or deleting nodes. Creating three unconnected nodes with values 1, 2, and 3:

每个节点都有一个值,并链接到下一个节点(假设有一个单链表),并且链表中唯一重要的变量是开始时的节点,因为它可以从该节点遍历直到到达另一个所需节点执行某些功能,例如在某些索引处添加或删除节点。 创建三个未连接的节点,其值分别为1、2和3:

node1 = Node(value=1)node2 = Node(value=2)node3 = Node(value=3)

They can be connected by setting their .next attributes. Note that in this case, we are creating a circular linked list (which can be repurposed as a queue).

可以通过设置.next属性来连接它们。 请注意,在这种情况下,我们正在创建一个循环链接列表(可以将其重新用作队列)。

node1.next = node2node2.next = node3node3.next = node1

Then, one can attach a head — in this case, simply a starting point — to create a linked list: linked_list = LinkedList (head=node1). From here, traversing is as simple as .next: linked_list.head.next.next.value will return 3, for instance. Deconstructing the command:

然后,可以附加一个头部(在这种情况下,只是起点)来创建一个链表: linked_list = LinkedList (head=node1) 。 从这里开始,遍历就像.next一样简单:例如, linked_list.head.next.next.value将返回3。 解构命令:

  • linked_list.head is node1.

    linked_list.headnode1

  • node1.next is node2.

    node1.nextnode2

  • node2.next is node3.

    node2.nextnode3

  • node3.value is 3.

    node3.value3

关键点 (Key Points)

  • A linked list is a special type of list in which elements are not stored in one contiguous location but as values with pointers, in which pointers link to the next element.链表是一种特殊的列表,其中元素不是存储在一个连续的位置中,而是与指针一起作为值存储,其中指针链接到下一个元素。
  • Linked lists are more efficient with operations like inserting and deleting, but are less efficient with accessing values, since a node can only be accessed by traversing through the linked list.链表在插入和删除等操作中效率更高,但在访问值时效率较低,因为只能通过遍历链表来访问节点。
  • A doubly linked list is a linked list with both a previous and a next pointer.双链表是具有上一个指针和下一个指针的链表。
  • A circular linked list is a linked list in which there does not exist a head or a tail because elements are linked such that the list loops.循环链表是一种链表,其中不存在头部或尾部,因为元素被链接以使列表循环。
  • A queue, which can implemented well with a circular linked list, is a data structure in which only two operations can be performed: enqueueing, which adds an element at the end of the queue, and dequeuing, which removes the first element in the queue.可以很好地用循环链表实现的队列是一种数据结构,其中只能执行两个操作:入队(在队列末尾添加一个元素)和出队(在队列中删除第一个元素) 。

All images created by author.

作者创作的所有图像。

翻译自: https://medium.com/swlh/arrays-2-0-linked-list-queue-data-structures-26a65d3551b5

数据结构栈和队列


http://www.taodudu.cc/news/show-863479.html

相关文章:

  • 轨迹预测演变(第1/2部分)
  • 人口预测和阻尼-增长模型_使用分类模型预测利率-第3部分
  • 机器学习 深度学习 ai_人工智能,机器学习,深度学习-特征和差异
  • 随机模拟_随机模拟可帮助您掌握统计概念
  • 机器学习算法如何应用于控制_将机器学习算法应用于NBA MVP数据
  • 知乎 开源机器学习_使用开源数据和机器学习预测海洋温度
  • :)xception_Xception:认识Xtreme盗梦空间
  • 评估模型如何建立_建立和评估分类ML模型
  • 介绍神经网络_神经网络介绍
  • 人物肖像速写_深度视频肖像
  • 奇异值值分解。svd_推荐系统-奇异值分解(SVD)和截断SVD
  • 机器学习 对模型进行惩罚_使用Streamlit对机器学习模型进行原型制作
  • 神经网络实现xor_在神经网络中实现逻辑门和XOR解决方案
  • sagan 自注意力_请使用英语:自我注意生成对抗网络(SAGAN)
  • pytorch 音频分类_Pytorch中音频的神经风格转换
  • 变压器 5g_T5:文本到文本传输变压器
  • 演示方法:有抱负的分析师
  • 机器学习 模型性能评估_如何评估机器学习模型的性能
  • 深度学习将灰度图着色_通过深度学习为视频着色
  • 工业机器人入门实用教程_机器学习实用入门
  • facebook 图像比赛_使用Facebook的Detectron进行图像标签
  • 营销大数据分析 关键技术_营销分析的3个最关键技能
  • ue4 gpu构建_待在家里吗 为什么不构建GPU Box!
  • 使用机器学习预测天气_使用机器学习的二手车价格预测
  • python集群_使用Python集群文档
  • 马尔可夫的营销归因
  • 使用Scikit-learn,Spotify API和Tableau Public进行无监督学习
  • 街景图像分割_借助深度学习和街景图像进行城市的大规模树木死亡率研究
  • 多目标分类的混淆矩阵_用于目标检测的混淆矩阵
  • 检测和语义分割_分割和对象检测-第2部分

数据结构栈和队列_使您的列表更上一层楼:链接列表和队列数据结构相关推荐

  1. 数据结构栈的知识_数据知识栈

    数据结构栈的知识 并发不适合胆小者 我们都知道并发编程很难正确实现. 这就是为什么在执行线程任务之后要进行大量的设计和代码审查会议. 您永远不会将并发问题分配给经验不足的开发人员. 仔细分析问题空间, ...

  2. 假设以带头结点的循环链表表示队列_真香!20张图揭开「队列」的迷雾,一目了然...

    https://mp.weixin.qq.com/s/GYQrxBOasKpvF4uZsMdOSw​mp.weixin.qq.com 队列的概念 首先我们联想一下链表,在单链表中,我们只能对他的链表表 ...

  3. html模板动画效果图,html5动画模板_使基本HTML模板更上一层楼的动画

    为了更轻松地处理动画,我首先想对不同的方面进行分类和组织. 每个动画将具有五个参数,并具有一系列潜在值: 类型 :单身,团体,相关  顺序 :顺序,随机,同时  事件 :加载,滚动,单击,悬停,调整大 ...

  4. html5动画模板_使基本HTML模板更上一层楼的动画

    html5动画模板 动画可以使您的网站演示文稿更上一层楼. 正确的动画将对您网站的用户体验产生积极的影响,加强每次互动并创造令人难忘的体验. 另一方面,选择不当的动画可能会破坏体验,迷惑用户并可能会降 ...

  5. python合并列表并按升序排序_在python中按升序合并两个排序的链接列表:单链接列表指针更新问题...

    你需要分配 l1 和 l2 tempNode.val L1 节点本身到 tempNode # Definition for singly-linked list. class ListNode: de ...

  6. 假设有搅乱顺序的一群儿童成一个队列_数据结构与算法系列之栈amp;队列(GO)...

    以下完整代码均可从这里获取 栈 栈的基本概念 「后进先出.先进后出就是典型的栈结构」.栈可以理解成一种受了限制的线性表,插入和删除都只能从一端进行 当某个数据集合只涉及在一端插入和删除数据,并且满足后 ...

  7. java数据结栈空的条件表达式_数据结构——栈和队列例题

    1.若一个栈的输入序列为1,2,3,-,n,输出序列的第一个元素是i,则第j个输出元素是_____. 选项ABCD均错误,第j个输出元素应为i-j+1. 栈是一种先进后出的数据结构,也就是说如果入栈顺 ...

  8. c++数据结构队列栈尸体_数据结构-栈与队列(二)

    1.设有编号为1,2,3,4 的四辆列车,顺序进入一个栈式结构的站台,如图3.90所示.具体写出这四辆列车开出车站的所有可能的顺序,设栈容量为2. 1234 1243 1324 1342 2134 2 ...

  9. 大话数据结构—栈与队列

    栈 一.栈的定义 栈是(stack)是限定尽在表尾进行插入和删除操作的线性表. 栈又称为后进先出(Last In First Out)的线性表,简称LIFO结构. 二.进栈出栈变化形式 注意: 并不是 ...

最新文章

  1. [JS] undefined、null、ReferenceError的区别、变量作用域问题
  2. 通过打印学习Linux内核之sysfs(0)
  3. cvm服务器怎么建网站,云服务器cvm快速入门教程
  4. 下载 | 新版Java开发手册有哪些亮点?
  5. 51 nod 1495 中国好区间 奇葩卡时间题 700ms 卡O(n*log(n)), 思路:O(n)尺取法
  6. NC / Netcat - 文件传输
  7. acm 3278(poj4001)
  8. 禁用驱动程序强制签名(终极办法)
  9. java毕向东学习笔记——day09
  10. 管家婆服务器端口修改,211端口被占用,如何设置
  11. 51单片机汇编语言实验及代码
  12. jk背带是什么意思_JK 制服和 LO 装 (科普向)
  13. Rxjava2中Single的just操作符源码学习
  14. Kibana server is not ready yet
  15. ACdream 1069 无耻的出题人 无聊写着玩的题
  16. 本地主机Xshell连接虚拟机Linux CentOS7
  17. idea中Rebuild是什么意思
  18. Tensorflow2训练Fer2013数据集
  19. 三本毕业后,选择了大数据开发职业
  20. 通过windows官网工具制作win10启动盘并安装win10系统

热门文章

  1. 安装Windows Nano Server虚拟机
  2. Android多媒体开发-- android中OpenMax的实现整体框架
  3. DOM节点中属性nodeName、nodeType和nodeValue的区别 Delphi
  4. 进一步:BSD信号和异常同时捕获
  5. 无线技术之WLAN八个常见问题解答(收藏)
  6. vue与php接口对接,怎样使用vue项目中api接口
  7. preparing automatic repair怎么解决_单一窗口插卡登录频繁提示安装IC卡控件的终极解决办法...
  8. mac 偏好设置mysql不小心删除了_Mac 安装配置mysql,误删除local下的var和tmp文件夹该如何解决?...
  9. C语言分支结构的作用,C语言丨用switch语句实现多分支选择结构
  10. [蓝桥杯][2018年第九届真题]整理玩具(树状数组)