日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构栈和队列_使您的列表更上一层楼:链接列表和队列数据结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構棧和隊列

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):

要在節點i和i + 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 = node2
node2.next = node3
node3.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.head是node1 。

  • node1.next is node2.

    node1.next是node2 。

  • node2.next is node3.

    node2.next是node3 。

  • node3.value is 3.

    node3.value是3 。

關鍵點 (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

數據結構棧和隊列

總結

以上是生活随笔為你收集整理的数据结构栈和队列_使您的列表更上一层楼:链接列表和队列数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。