![Chapter 3 Linked List_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/395d057b-31b8-424d-9f84-7555a17c8ca8/395d057b-31b8-424d-9f84-7555a17c8ca81.gif)
![Chapter 3 Linked List_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/395d057b-31b8-424d-9f84-7555a17c8ca8/395d057b-31b8-424d-9f84-7555a17c8ca82.gif)
![Chapter 3 Linked List_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/395d057b-31b8-424d-9f84-7555a17c8ca8/395d057b-31b8-424d-9f84-7555a17c8ca83.gif)
![Chapter 3 Linked List_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/395d057b-31b8-424d-9f84-7555a17c8ca8/395d057b-31b8-424d-9f84-7555a17c8ca84.gif)
![Chapter 3 Linked List_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/395d057b-31b8-424d-9f84-7555a17c8ca8/395d057b-31b8-424d-9f84-7555a17c8ca85.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 In computer science, a linked list is one of the fundamental data structures used in computer programming. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references (links) pointing to the next and/or previous nodes. 在計算機科學(xué)中,鏈表在計算機編程中是基在計算機科學(xué)中,鏈表在計算機編程中是基本的
2、數(shù)據(jù)結(jié)構(gòu)之一。它由節(jié)點的序列組成,本的數(shù)據(jù)結(jié)構(gòu)之一。它由節(jié)點的序列組成,每個節(jié)點包含任意數(shù)據(jù)字段和一個或兩個鏈每個節(jié)點包含任意數(shù)據(jù)字段和一個或兩個鏈接來指向下一個和接來指向下一個和/或前一個節(jié)點?;蚯耙粋€節(jié)點。 A linked list is a self-referential datatype because it contains a pointer or link to another data of the same type. Linked lists permit insertion and removal of nodes at any point in the list i
3、n constant time, but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists. 鏈表是一種自我引用的數(shù)據(jù)類型,因為它包含一鏈表是一種自我引用的數(shù)據(jù)類型,因為它包含一個指針或鏈接到另一個相同類型的數(shù)據(jù)。鏈表允個指針或鏈接到另一個相同類型的數(shù)據(jù)。鏈表允許在常數(shù)時間內(nèi)插入和刪除節(jié)點,但不允許隨機許在常數(shù)時間內(nèi)插入和刪除節(jié)點,但不允許隨機訪問。有幾種不同
4、類型的鏈表存在:單鏈表,雙訪問。有幾種不同類型的鏈表存在:單鏈表,雙鏈表和循環(huán)鏈表。鏈表和循環(huán)鏈表。 Linked lists can be implemented in most languages. Languages such as Lisp and Scheme have the data structure built in, along with operations to access the linked list. Procedural languages such as C, C+, and Java typically rely on mutable references
5、 to create linked lists. 鏈表在大多數(shù)語言中能被實現(xiàn)。如鏈表在大多數(shù)語言中能被實現(xiàn)。如Lisp(鏈(鏈表處理語言)和表處理語言)和Scheme(一種系統(tǒng)設(shè)計語(一種系統(tǒng)設(shè)計語言)語言有內(nèi)置的數(shù)據(jù)結(jié)構(gòu),以及訪問鏈表言)語言有內(nèi)置的數(shù)據(jù)結(jié)構(gòu),以及訪問鏈表的操作。程序語言例如的操作。程序語言例如C,C+和和Java通常通常依賴于可變的引用來創(chuàng)建鏈表。依賴于可變的引用來創(chuàng)建鏈表。 1.Variants 變量變量 1.1 Linearly-linked List線性鏈表線性鏈表 Singly-linked List單鏈表單鏈表 The simplest kind of linke
6、d list is a singly-linked list (or slist for short), which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node. 最簡單的鏈表是一個單鏈表(或最簡單的鏈表是一個單鏈表(或 ),其中每個節(jié)點),其中每個節(jié)點都有一個鏈接。這個鏈接指向鏈表中的下一個節(jié)點,都有一個鏈接。這個鏈接指向鏈表中的下一個節(jié)點,或者如果它是最后一個節(jié)點,則指向一個空值或空
7、鏈或者如果它是最后一個節(jié)點,則指向一個空值或空鏈表。表。 Doubly-linked List雙鏈表雙鏈表 A more sophisticated kind of linked list is a doubly-linked list or two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or poi
8、nts to a null value or empty list if it is the final node. 更為復(fù)雜的一種鏈表是雙鏈表或雙向鏈表。更為復(fù)雜的一種鏈表是雙鏈表或雙向鏈表。每個節(jié)點有兩個鏈接:一個指向前一個節(jié)點,每個節(jié)點有兩個鏈接:一個指向前一個節(jié)點,或若它是第一個節(jié)點則指向一個空值或空鏈表,或若它是第一個節(jié)點則指向一個空值或空鏈表,以及另一個指向下一個節(jié)點,或若它是最后一以及另一個指向下一個節(jié)點,或若它是最后一個節(jié)點則指向一個空值或空鏈表。個節(jié)點則指向一個空值或空鏈表。 In some very low level languages, Xor-linking offe
9、rs a way to implement doubly-linked lists using a single word for both links, although the use of this technique is usually discouraged. 在一些非常低級別的語言中,在一些非常低級別的語言中,Xor-linking提供了使用單個字用于雙向連接提供了使用單個字用于雙向連接的方法來實現(xiàn)雙向鏈表,盡管這一技術(shù)的方法來實現(xiàn)雙向鏈表,盡管這一技術(shù)的使用通常是不受鼓勵的。的使用通常是不受鼓勵的。1.2 Circularly-linked List循環(huán)鏈表循環(huán)鏈表 In a
10、circularly-linked list, the first and final nodes are linked together. This can be done for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node. 在循環(huán)鏈表中,第一個和最后的節(jié)點是相連的。在循環(huán)鏈表中,第一個和最后
11、的節(jié)點是相連的。這可為單和雙鏈表。遍歷循環(huán)鏈表,你可以在這可為單和雙鏈表。遍歷循環(huán)鏈表,你可以在任何一個節(jié)點開始,然后按照鏈表中的任何一任何一個節(jié)點開始,然后按照鏈表中的任何一個方向訪問,直到返回到原來的節(jié)點。個方向訪問,直到返回到原來的節(jié)點。 Viewed another way, circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest, and in cases where
12、you have one object in a list and wish to see all other objects in the list.從另一個方面從另一個方面來說,循環(huán)鏈表可以被看作是無始無終。這來說,循環(huán)鏈表可以被看作是無始無終。這種類型的鏈表在負責(zé)管理數(shù)據(jù)保存緩沖區(qū)方種類型的鏈表在負責(zé)管理數(shù)據(jù)保存緩沖區(qū)方面是最有用的,在你的鏈表有一個對象的情面是最有用的,在你的鏈表有一個對象的情況,并希望看到鏈表中的所有其他對象。況,并希望看到鏈表中的所有其他對象。 The pointer pointing to the whole list is usually called the
13、end pointer.指向整個鏈表指針通常被稱為指向整個鏈表指針通常被稱為尾指針。尾指針。 Singly-circularly-linked List單循環(huán)鏈表單循環(huán)鏈表 In a singly-circularly-linked list, each node has one link, similar to an ordinary singly-linked list, except that the next link of the last node points back to the first node. As in a singly-linked list, new node
14、s can only be efficiently inserted after a node we already have a references to. 在單循環(huán)鏈在單循環(huán)鏈表中,每個節(jié)點有一個鏈接,類似于一個普通的單鏈表中,每個節(jié)點有一個鏈接,類似于一個普通的單鏈表,除了最后一個節(jié)點的下一個鏈接返回到第一個節(jié)表,除了最后一個節(jié)點的下一個鏈接返回到第一個節(jié)點。由于在單鏈表中,新的節(jié)點只能被有效地插入到點。由于在單鏈表中,新的節(jié)點只能被有效地插入到一個我們已經(jīng)引用的節(jié)點后。一個我們已經(jīng)引用的節(jié)點后。 For this reason, its usual to retain a refer
15、ence to only the last element in a singly-circularly-linked list, as this allows quick insertion at the beginning, and also allows access to the first node through the last nodes next pointer. 出于這個原因,通常要保留引用的單循環(huán)鏈出于這個原因,通常要保留引用的單循環(huán)鏈表中的最后一個元素,因為這允許在表的開表中的最后一個元素,因為這允許在表的開始處快速插入,并允許通過最后一個節(jié)點的始處快速插入,并允許通過
16、最后一個節(jié)點的下一個指針指向第一個節(jié)點。下一個指針指向第一個節(jié)點。 Doubly-circularly-linked List雙循環(huán)鏈表雙循環(huán)鏈表 In a doubly-circularly-linked list, each node has two links, similar to a doubly-linked list, except that the previous link of the first node points to the last node and the next link of the last node points to the first node.
17、 As in doubly-linked lists, insertions and removals can be done at any point with access to any nearby node. 在雙向循環(huán)鏈表中,每個節(jié)點有兩個鏈接,類似在雙向循環(huán)鏈表中,每個節(jié)點有兩個鏈接,類似于一個雙鏈表,除了第一個節(jié)點的前一個鏈接到于一個雙鏈表,除了第一個節(jié)點的前一個鏈接到最后一個節(jié)點,以及最后一個節(jié)點的下一個鏈接最后一個節(jié)點,以及最后一個節(jié)點的下一個鏈接到第一個節(jié)點。正如在雙向鏈表中,插入和刪除到第一個節(jié)點。正如在雙向鏈表中,插入和刪除可以在任何時候訪問附近的任何節(jié)點來完成??梢栽?/p>
18、任何時候訪問附近的任何節(jié)點來完成。 1.3 Sentinel sentn()l Nodes哨兵結(jié)哨兵結(jié)點點 Linked lists sometimes have a special dummy or sentinel node at the beginning and/or at the end of the list, which is not used to store data. 鏈表有時在鏈表頭和鏈表有時在鏈表頭和/或鏈表尾帶有一個特或鏈表尾帶有一個特殊虛擬的哨兵結(jié)點,這個結(jié)點不用來存儲數(shù)殊虛擬的哨兵結(jié)點,這個結(jié)點不用來存儲數(shù)據(jù)。據(jù)。 Its purpose is to simpli
19、fy or speed up some operations, by ensuring that every data node always has a previous and/or next node, and that every list (even one that contains no data elements) always has a “first” and “l(fā)ast” node.它的目它的目的是為了簡化或加速某些操作,確保每個數(shù)據(jù)的是為了簡化或加速某些操作,確保每個數(shù)據(jù)結(jié)點都有一個前繼和結(jié)點都有一個前繼和/或一個后繼結(jié)點,也能或一個后繼結(jié)點,也能使每個鏈表(甚至是沒有
20、數(shù)據(jù)元素的鏈表)都使每個鏈表(甚至是沒有數(shù)據(jù)元素的鏈表)都有一個頭結(jié)點和尾結(jié)點。有一個頭結(jié)點和尾結(jié)點。 Lisp has such a designthe special value nil is used to mark the end of a “proper” singly-linked list, or chain of cons cells as they are called. A list does not have to end in nil, but such a list would be termed “improper”. Lisp語言有如此的設(shè)計語言有如此的設(shè)計一個特
21、殊值一個特殊值nil被用被用來標(biāo)記所謂的來標(biāo)記所謂的“恰當(dāng)恰當(dāng)”單鏈表的表尾,或者正單鏈表的表尾,或者正如被稱作列表單元格的鏈。一個鏈表不一定要如被稱作列表單元格的鏈。一個鏈表不一定要以以nil結(jié)尾,但是這樣的鏈表將被稱為結(jié)尾,但是這樣的鏈表將被稱為“不恰不恰當(dāng)?shù)漠?dāng)?shù)摹薄?2.Applications of Linked Lists Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations. 鏈表可用做其他數(shù)據(jù)類型的構(gòu)
22、件,比如堆棧,隊列以及鏈表可用做其他數(shù)據(jù)類型的構(gòu)件,比如堆棧,隊列以及它們的變體。它們的變體。 The “data” field of a node can be another linked list. By this device, one can construct many linked data structures with lists; 節(jié)點的數(shù)據(jù)區(qū)可以是另外一個鏈表。通過這種機節(jié)點的數(shù)據(jù)區(qū)可以是另外一個鏈表。通過這種機制,人們使用表可以構(gòu)建各種連接數(shù)據(jù)結(jié)構(gòu)制,人們使用表可以構(gòu)建各種連接數(shù)據(jù)結(jié)構(gòu); this practice originated in the Lisp prog
23、ramming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style. 這種用法起源于這種用法起源于Lisp編程語言,在這種語言中,鏈編程語言,在這種語言中,鏈表是主要的(雖然并非唯一的)數(shù)據(jù)結(jié)構(gòu),而且是表是主要的(雖然并非唯一的)數(shù)據(jù)結(jié)構(gòu),而且是現(xiàn)在一種常見的函數(shù)式編程風(fēng)格的特征?,F(xiàn)在一種常見的函數(shù)式編程風(fēng)格的特征。 Sometimes, li
24、nked lists are used to implement associative arrays, and are in this context called associations lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary trees even on small data sets (see the disc
25、ussion in associative array) .有時,鏈表用以實現(xiàn)關(guān)聯(lián)數(shù)組,有時,鏈表用以實現(xiàn)關(guān)聯(lián)數(shù)組,在這時候被稱為關(guān)聯(lián)表。鏈表的這種用法沒有一在這時候被稱為關(guān)聯(lián)表。鏈表的這種用法沒有一點好處,他們?nèi)菀妆黄渌臄?shù)據(jù)結(jié)構(gòu)所超越,比點好處,他們?nèi)菀妆黄渌臄?shù)據(jù)結(jié)構(gòu)所超越,比如自平衡二叉搜索樹,即使是在小數(shù)據(jù)集方面如自平衡二叉搜索樹,即使是在小數(shù)據(jù)集方面(可查看關(guān)聯(lián)數(shù)組討論)。(可查看關(guān)聯(lián)數(shù)組討論)。 However, sometimes a linked lists is dynamically created out of nodes in such a tree, and use
26、d to more efficiently traverse that set. 然而,有些時候,鏈表在這樣的一個然而,有些時候,鏈表在這樣的一個樹中是動態(tài)創(chuàng)建節(jié)點的,而且也常用樹中是動態(tài)創(chuàng)建節(jié)點的,而且也常用于高效的遍歷數(shù)據(jù)集。于高效的遍歷數(shù)據(jù)集。 3 .Linked List Operations鏈表操作鏈表操作 When manipulating linked lists in-place ,care must be taken to not use values that you have invalidated in previous assignments .This makes
27、algorithms for inserting or deleting linked list node somewhat subtle . 當(dāng)操作鏈表時,不能使用先前已經(jīng)作廢的數(shù)值。這使得當(dāng)操作鏈表時,不能使用先前已經(jīng)作廢的數(shù)值。這使得節(jié)點的插入刪除有些微妙(難以把握)。節(jié)點的插入刪除有些微妙(難以把握)。 This section gives pseudocode for adding or removing nodes from singly, doubly, and circularly linked lists in-place. Throughout we will use nu
28、ll to refer to an end-of-list marker or sentinel, which may be implemented in a number of ways. 這部分給出了偽代碼,用于實現(xiàn)從單向,雙向這部分給出了偽代碼,用于實現(xiàn)從單向,雙向循環(huán)鏈表中添加,刪除節(jié)點的操作。自始至終,循環(huán)鏈表中添加,刪除節(jié)點的操作。自始至終,我們使用空值表示鏈表的結(jié)束或者哨兵,這我們使用空值表示鏈表的結(jié)束或者哨兵,這些標(biāo)志可以使用多種方法實現(xiàn)。些標(biāo)志可以使用多種方法實現(xiàn)。 3.1 Linearly-linked Lists 線性鏈表線性鏈表 Singly-linked Lists
29、Our node data structure will have two fields. We also keep a variable firstNode which always points to the first node in the list, or is null for an empty list. 我們的節(jié)點數(shù)據(jù)結(jié)構(gòu)將有兩個字段。我們設(shè)我們的節(jié)點數(shù)據(jù)結(jié)構(gòu)將有兩個字段。我們設(shè)置一個變量叫置一個變量叫firstNode,它總是指向列表,它總是指向列表中的第一個節(jié)點,或者對于一個空列表來說中的第一個節(jié)點,或者對于一個空列表來說它為空。它為空。 record Node data
30、/ The data being stored in the node 存儲在節(jié)點中的數(shù)據(jù)存儲在節(jié)點中的數(shù)據(jù) next / A reference to the next node; null for last node指向下一個節(jié)點的引用,對于指向下一個節(jié)點的引用,對于最后一個節(jié)點來說為空最后一個節(jié)點來說為空 record List Node firstNode / points to first node of list; null for empty list指向鏈表中的第一個指向鏈表中的第一個節(jié)點節(jié)點;對于空表來說為空對于空表來說為空 Traversal of a singly-lin
31、ked list is easy, beginning at the first node and following each next link until we come to the end: 一個單鏈表的遍歷很容易,從第一個節(jié)點開一個單鏈表的遍歷很容易,從第一個節(jié)點開始,接著依次順序遍歷后面的每個節(jié)點,直始,接著依次順序遍歷后面的每個節(jié)點,直到結(jié)束:到結(jié)束: node: = list. firstNode while node not null ( do something with node. data) node: =node. Next The following code i
32、nserts a node after an existing node in a singly linked list. The diagram shows how it works. Inserting a node before an existing one cannot be done; instead, you have to locate it while keeping track of the previous node. 下面的代碼把一個節(jié)點插入到一個單向鏈表下面的代碼把一個節(jié)點插入到一個單向鏈表中的一個現(xiàn)有節(jié)點后。下圖顯示了它是如何中的一個現(xiàn)有節(jié)點后。下圖顯示了它是如何工
33、作的。在一個現(xiàn)有的節(jié)點之前插入一個新工作的。在一個現(xiàn)有的節(jié)點之前插入一個新節(jié)點是不被允許的。相反的,你必須找到它節(jié)點是不被允許的。相反的,你必須找到它的位置,并且跟蹤它前一個節(jié)點。的位置,并且跟蹤它前一個節(jié)點。 function insertAfter ( Node node, Node newNode) /insert newNode after node 在原有的節(jié)點后插入新節(jié)點在原有的節(jié)點后插入新節(jié)點 newNode. Next: =node. Next node. next: =newNode Inserting at the beginning of the list require
34、s a separate function. This requires updating first-Node. 在列表的開頭插入一個節(jié)點需要一個單獨的函數(shù)。這首先需要更在列表的開頭插入一個節(jié)點需要一個單獨的函數(shù)。這首先需要更新第一個節(jié)點。新第一個節(jié)點。 function insertBeginning ( List list, Node newNode) /insert node before current first node 在當(dāng)前第一個節(jié)點之前插入一個節(jié)點在當(dāng)前第一個節(jié)點之前插入一個節(jié)點 newNode. next: =list. firstNode list. firstNode
35、 : =newNode Similarly, we have functions for removing the node after a given node, and for removing a node from the beginning of the list. The diagram demonstrates the former. To find and remove a particular node, one must again keep track of the previous element. 同樣地,我們也有在給定的節(jié)點后刪除一個節(jié)點的函同樣地,我們也有在給定的
36、節(jié)點后刪除一個節(jié)點的函數(shù),以及在列表的開頭刪除一個節(jié)點的函數(shù)。下圖數(shù),以及在列表的開頭刪除一個節(jié)點的函數(shù)。下圖演示了前者。要查找并刪除一個特定的節(jié)點,也必演示了前者。要查找并刪除一個特定的節(jié)點,也必須跟蹤它的前一個元素。須跟蹤它的前一個元素。 function removeAfter ( Node node) /remove node past this one 刪除在指定節(jié)點后的那個節(jié)點刪除在指定節(jié)點后的那個節(jié)點 obsoleteNode: =node. Next node. next : =node. next. next destroy obsoleteNode function rem
37、oveBeginning ( List list) /remove first node 刪除第一個節(jié)點刪除第一個節(jié)點 obsoleteNode: =list. firstNode list. firstNode : =list. firstNode. next /point past deleted node and destroy obsoleteNode 指向已經(jīng)刪除的節(jié)點并銷毀它指向已經(jīng)刪除的節(jié)點并銷毀它 Notice that removeBeginning sets list. firstNode to null when removing the last node in the
38、 list. 請注意函數(shù)請注意函數(shù)removeBeginning需要建需要建表。當(dāng)刪除列表中的最后一個節(jié)點時表。當(dāng)刪除列表中的最后一個節(jié)點時第一個節(jié)點為空。第一個節(jié)點為空。 The algorithms above can be made simpler if one puts a dummy element at the front of the list. That way, one never needs to insert or remove at the beginning, thus making insertBeginning and removeBeginning unnece
39、ssary. Since we cant iterate backwards, efficient insertBefore or removeBefore operations are not possible. 如果在列表的前面放置一個虛擬的元素如果在列表的前面放置一個虛擬的元素,那么上面的那么上面的算法將變得更簡單。這樣一來就絕不需要在列表開頭算法將變得更簡單。這樣一來就絕不需要在列表開頭插入或刪除元素,從而插入或刪除元素,從而insertBeginning和和removeBeginning函數(shù)就變的不必要了。由于我們函數(shù)就變的不必要了。由于我們不能重復(fù)向后,因此有效的不能重復(fù)向后,因此
40、有效的“insertBefore”或或“removeBefore”操作是不可能的。操作是不可能的。 Appending one linked list to another is also inefficient, because we must traverse the entire first list in order to find its tail, and then append the second list to this. Thus , if two linearly-linked lists are each of length n, list appending has
41、asymptotic time complexity of O (n) . In the Lisp family of languages, list appending is provided by the append procedure. 附加一個鏈表到另一個鏈表也是低效的,因為我附加一個鏈表到另一個鏈表也是低效的,因為我們必須遍歷整個第一個鏈表,以便找到它的尾,們必須遍歷整個第一個鏈表,以便找到它的尾,然后把第二個列表附加到第一個列表的尾部。因然后把第二個列表附加到第一個列表的尾部。因此,如果兩個線性鏈表的長度都為此,如果兩個線性鏈表的長度都為n,那么附加,那么附加鏈表的時間復(fù)雜度是線
42、性時間鏈表的時間復(fù)雜度是線性時間O(n)。在)。在Lisp語言中,附加列表由附加程序來提供實現(xiàn)。語言中,附加列表由附加程序來提供實現(xiàn)。 Doubly-linked Lists雙向鏈表雙向鏈表 With doubly-linked lists there are even more pointers to update, but also less information is needed, since we can use backwards pointers to observe preceding elements in the list. This enables new operat
43、ions, and eliminates special-case functions. 雙向鏈表有更多的指針執(zhí)行更新操作,但是所雙向鏈表有更多的指針執(zhí)行更新操作,但是所需要的信息更少了,我們可以通過反向指針來需要的信息更少了,我們可以通過反向指針來查找前面的元素。它擁有新的操作,并且有消查找前面的元素。它擁有新的操作,并且有消除特殊情況的功能。除特殊情況的功能。 We will add a prev filed to our nodes, pointing to the previous element, and a lastNode field to our list structure
44、which always points to the last node in the list. Both list. FirstNode and list. LastNode are null for an empty list. 我們將添加一個指向前一個元素我們將添加一個指向前一個元素prev字段字段到節(jié)點中,并添加一個總是指向鏈表的最后到節(jié)點中,并添加一個總是指向鏈表的最后一個節(jié)點的一個節(jié)點的lastNode字段。列表的對一個空字段。列表的對一個空鏈表而言,鏈表而言,F(xiàn)irstNode和和LastNoded都是空都是空的。的。Record NodeData/The data being
45、 stored in the nodeNext/A reference to the next node; null for last nodePrev/A reference to the previous node; null for first nodeRecord ListNode firstNode/points to first node of list; null for empty listNode lastNode/points to last node of list; null for empty list Iterating through a doubly linke
46、d list can be done in either direction. In fact, direction can change many times, if desired. Forwards Node := list . firstNode While nodenull Node := node.next Backwards Node:=list.lastNode While nodenull Node:=node.prev記錄節(jié)點記錄節(jié)點 data/存儲節(jié)點中的數(shù)據(jù)存儲節(jié)點中的數(shù)據(jù) Next/下一節(jié)點的引用;空則為末節(jié)點下一節(jié)點的引用;空則為末節(jié)點 Prev/前一節(jié)點的引用;空
47、則為首節(jié)點前一節(jié)點的引用;空則為首節(jié)點 記錄鏈表記錄鏈表 Node firstNode/指向表中的的第一個節(jié)點;空則為指向表中的的第一個節(jié)點;空則為空表空表 Node lastNode/指向表中的最后一個節(jié)點;空則為空指向表中的最后一個節(jié)點;空則為空表表 循環(huán)訪問可以在雙鏈表的任意一個方向上進行,循環(huán)訪問可以在雙鏈表的任意一個方向上進行,事實上,如果想的話,方向可以改變很多次。事實上,如果想的話,方向可以改變很多次。向前向前指向首節(jié)點指向首節(jié)點 當(dāng)節(jié)點不為空當(dāng)節(jié)點不為空 后移后移向后向后指向末節(jié)點指向末節(jié)點當(dāng)節(jié)點不為空當(dāng)節(jié)點不為空 前移前移 These symmetric functions
48、add a node either after or before a given node, with the diagram demonstrating after:對稱功對稱功能增加的結(jié)點可以在所給的結(jié)點前面也可以能增加的結(jié)點可以在所給的結(jié)點前面也可以在所給的結(jié)點后面。在所給的結(jié)點后面。 3.2 Circularly-linked Lists循環(huán)鏈表循環(huán)鏈表 Circularly-linked lists can be either singly or doubly-linked. In a circularly-linked list, all node are linked in a
49、 continuous circle, without using null. 循環(huán)鏈表既不是單向鏈表也不是雙向鏈表。在循環(huán)鏈表既不是單向鏈表也不是雙向鏈表。在循環(huán)鏈表中,所以的結(jié)點都連接成一個循環(huán)鏈,循環(huán)鏈表中,所以的結(jié)點都連接成一個循環(huán)鏈,不使用空結(jié)點。不使用空結(jié)點。 For lists with a front and back (such a queue), one stores a reference to the last node in the list. The next node after the last node is the first node. Elements
50、can be added to the back of the list and removed from the front in constant time. 對于鏈表一前一后的連接,最后一個結(jié)點用作對于鏈表一前一后的連接,最后一個結(jié)點用作參考。最后一個結(jié)點的下一個結(jié)點是下一個鏈參考。最后一個結(jié)點的下一個結(jié)點是下一個鏈表的第一個結(jié)點。元素可以添加到鏈表的后面表的第一個結(jié)點。元素可以添加到鏈表的后面并且可以刪除常數(shù)。并且可以刪除常數(shù)。 Both types of circularly-linked lists benefit from the ability to traverse the
51、full list beginning at any given node. This often allows us to avoid storing firstNode and lastNode, although if the list may be empty we need a special representation for the empty list, such as a lastNode variable which points to some list in the node or is null if it is empty; we use such a lastN
52、ode here. 兩種類型的循環(huán)鏈表有利于將給定的結(jié)點添加兩種類型的循環(huán)鏈表有利于將給定的結(jié)點添加到滿鏈表中。這避免了我們存儲首尾結(jié)點,并到滿鏈表中。這避免了我們存儲首尾結(jié)點,并且如果鏈表為空我們需要一個特殊的代表來作且如果鏈表為空我們需要一個特殊的代表來作為空鏈表,例如指向鏈表的尾結(jié)點變量或者是為空鏈表,例如指向鏈表的尾結(jié)點變量或者是空結(jié)點;我們在這里使用這樣的尾結(jié)點??战Y(jié)點;我們在這里使用這樣的尾結(jié)點。 This representation significantly simplifies adding and removing nodes with a non-empty list,
53、 but empty lists are then a special case. 這代表著在一個非空鏈表中可以增加或刪除結(jié)這代表著在一個非空鏈表中可以增加或刪除結(jié)點,但是空鏈表是一個特殊的情況。點,但是空鏈表是一個特殊的情況。 Doubly- circularly-linked Lists雙向循環(huán)鏈雙向循環(huán)鏈表表 Assuming that someNode is some node in a non-empty list, this code iterates through that list starting with some node(any node will do): 假設(shè)在一個非空鏈表中某些結(jié)點是部分結(jié)點,假設(shè)在一個非空鏈表中某些結(jié)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國糖酰氯數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國電風(fēng)扇不可拆線插頭數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國水箱和水槽用承燒板數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國化纖針織布數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國全透明石材膠數(shù)據(jù)監(jiān)測研究報告
- 中樂器制作質(zhì)量管理與控制考核試卷
- 絲印染在環(huán)保包裝材料的創(chuàng)新應(yīng)用考核試卷
- 2025-2030年復(fù)古摩托車攝影基地企業(yè)制定與實施新質(zhì)生產(chǎn)力戰(zhàn)略研究報告
- 2025-2030年地鐵站行李自動搬運服務(wù)企業(yè)制定與實施新質(zhì)生產(chǎn)力戰(zhàn)略研究報告
- 2025-2030年復(fù)古射擊投幣樂行業(yè)跨境出海戰(zhàn)略研究報告
- 橋梁頂升移位改造技術(shù)規(guī)范
- 六年級語文(上冊)選擇題集錦
- 介紹人提成方案
- 天津在津居住情況承諾書
- PHOTOSHOP教案 學(xué)習(xí)資料
- 初中數(shù)學(xué)教學(xué)“教-學(xué)-評”一體化研究
- 2012年安徽高考理綜試卷及答案-文檔
- 《游戲界面設(shè)計專題實踐》課件-知識點5:圖標(biāo)繪制準(zhǔn)備與繪制步驟
- 自動扶梯安裝過程記錄
- MOOC 材料科學(xué)基礎(chǔ)-西安交通大學(xué) 中國大學(xué)慕課答案
- 智慧供熱管理系統(tǒng)方案可行性研究報告
評論
0/150
提交評論