线性结构循环队列_线性数据结构:JS中的链接列表,堆栈和队列

线性结构循环队列

by Yung L. Leung

梁永良

线性数据结构:JS中的链接列表,堆栈和队列 (Linear Data Structures: Linked Lists, Stacks, and Queues in JS)

Building from Simple Algorithms & Data Structures in JS, here we’ll look at data structures beyond arrays and key-value objects, beyond “labelled & deposit” boxes. Like a road along a path, linked lists, stacks & queues are direct ways to move from one unit of data to the next.

JS中的简单算法和数据结构构建 ,这里我们将讨论数组和键值对象之外的数据结构,以及“标签和存放”框之外的数据结构。 就像沿着道路的道路一样, 链表堆栈队列是从一个数据单元移动到另一个数据单元的直接方法。

链表 (Linked Lists)

A linked list is like a set of boxes chained together and stored in a dark room. To get to any one box requires starting at one end (head or tail) and following the links, from one box to the next. Upon arriving at any one box, you are pointed towards the direction of the next box. There is no index to act as a guide for leaping to any one box.

链表就像一组盒子在一起,被链接在一起并存储在黑暗的房间中。 要到达任何一个框,都需要从一个端点(头或尾)开始,并跟随链接,从一个框到下一个框。 到达任何一个盒子时,您都将指向下一个盒子的方向。 没有索引可作为跳到任一框的指南。

You can easily unshift or push “boxes” to the head or tail of the linked list. You can also easily shift or pop off “boxes” from the head or tail. The head or tail are easily accessible. But, to insert or remove “boxes” along the body of this linked list, to set items into a “box” that is beyond the head or tail, is more difficult. It requires starting at the head (or tail) and moving from a current “box” to the next one, before you can get to your desired “box.”

您可以轻松地 “框” 取消移动推入链接列表的开头或结尾。 您还可以轻松地从头或尾移动弹出 “框”。 头部或尾部很容易接近。 但是,要沿着此链接列表的正文插入删除 “框”, 项目放入头或尾以外的“盒子”中比较困难。 它要求从头(或尾)开始,然后从当前的“框”移动到下一个框,然后才能到达所需的“框”。

A singly linked list is a one-way linked list. This means that you can only move forward from the head to the tail. The complexity to unshift & shift is a constant (O(1)). This is because adding a “box” to or removing it from the beginning requires only accessing the head of the list. The complexity to push a “box” to the end is also O(1) for a similar reason, the tail is immediately accessible.

单链表是单向链表。 这意味着您只能从头到尾向前移动。 的到不印字移位复杂性是一个常数(O(1))。 这是因为在开始处添加或删除“框”仅需要访问列表的开头。 出于类似的原因,将“盒子” 到末尾的复杂度也是O(1) ,尾部可以立即使用。

But, to pop off the tail requires reassigning a new tail, which is reachable only by moving forward starting from the head, hence a linear complexity (O(n)). An n number of “boxes” requires n number of steps (operations) to reach the second to last “box” & reassign it as the new tail. Similarly, to insert/remove a “box,” or to get/set items in any “box” along the body of a list requires traveling from the head, and so their complexities are in general O(n).

但是,要弹出尾巴,需要重新分配一条新尾巴,这只能通过从头开始向前移动才能实现,因此线性复杂度( O(n) )。 n个“盒子”需要n个步骤(操作)才能到达倒数第二个“盒子”并将其重新分配为新的尾巴。 同样,要插入/删除 “框”或沿列表的主体在任何“框”中获取/设置项目,都需要从头开始,因此它们的复杂度通常为O(n)

A doubly linked list is a two-way linked list. This means you can move forward from the head or the tail. An advantage is that both the head & tail are easily accessible to add “boxes” to or remove “boxes” from. The complexity to unshift, shift, push or pop is O(1). The new tail required for popping a tail off is reachable from the current tail.

双链表是双向链表。 这意味着您可以从头或尾向前移动。 优点是头部和尾部都易于访问,可以在其中添加“框”或从中删除“框”。 取消 移位移位推动弹出的复杂度为O(1) 。 从当前尾巴可以弹出尾巴所需的新尾巴。

Another advantage of being able to travel from two different end points (head or tail) is that to insert/remove any “box” or to get/set “items” in a “box” along the body of a list takes half the time of a singly linked list. That is, their complexities are half of O(n). If the “box” or “item” is located at the 2nd half of the list, traveling from the tail will not require traveling through the 1st half of the list. The opposite is also true. Although, an O(1/2 n) tends to be simplified as O(n).

能够从两个不同的端点(头或尾)移动的另一个优点是,沿着列表的主体插入/删除任何“框”或在“框”中获取/设置 “项”需要花费一半的时间单链列表。 也就是说,它们的复杂度是O(n)的一半 。 如果“盒子”或“物品”位于列表的第二部分,则从尾部行驶将不需要通过列表的第一部分。 反之亦然。 虽然, O(1/2 n)倾向于简化为O(n)

堆栈 (Stacks)

A stack is a pile of items neatly arranged atop each other. A new item can be pushed onto the top of a stack, one at a time, building to the height of the stack. Reversely, each item can be popped off, one at a time, from the top of the stack. The last item in is always the first item out (LIFO).

堆栈是指整齐地排列在一起的一堆物品。 可以一次一个新物品到堆栈的顶部,直到堆栈的高度。 相反,可以从堆栈顶部一次弹出每个项目。 输入的最后一项始终是输出的第一项( LIFO )。

Since a stack operates by a LIFO process, the complexity to push an item to the top of the stack or to pop it off is a constant of O(1). The top of the stack is easily accessible.

由于堆栈通过LIFO流程进行操作,因此项目到堆栈顶部或将其弹出的复杂度是O(1)的常数。 堆栈的顶部易于访问。

Queue列 (Queues)

A queue is a line of items, neatly arranged next to each other. A new item can be enqueued to the end of the line, one at a time, elongating the line. Reversely, each item can be dequeued from the front of the line, one at a time. The first item in is always the first item out (FIFO).

队列是一排物品,彼此整齐地排列。 可以将一个新项目排队到该行的末尾,一次一次,延长该行。 相反,每个项目可以一次一行中出队 。 首项始终是首项( FIFO )。

Since both the front & end of the line is easily accessible, a queue’s enqueue & dequeue have a complexity of O(1).

由于行的前端和后端都很容易访问,因此队列的入出队的复杂度为O(1)

Thanks for reading!

谢谢阅读!

参考: (Reference:)

https://www.udemy.com/js-algorithms-and-data-structures-masterclass/

https://www.udemy.com/js-algorithms-and-data-structures-masterclass/

翻译自: https://www.freecodecamp.org/news/linear-data-structures-linked-lists-stacks-and-queues-in-javascript-a13c7591ad87/

线性结构循环队列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值