數據結構教程網盤鏈接_數據結構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.pswp.cn/news/393330.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393330.shtml
英文地址,請注明出處:http://en.pswp.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…