|
| 1 | +# リンクリスト |
| 2 | + |
| 3 | +コンピュータサイエンスにおいて、**リンクリスト**はデータ要素の線形コレクションです。要素の順番はメモリ内の物理的な配置によっては決まりません。代わりに、各要素が次の要素を指しています。リンクリストはノードのグループからなるデータ構造です。最も単純な形式では、各ノードはデータとシーケンス内における次のノードへの参照(つまり、リンク)で構成されています。この構造はイテレーションにおいて任意の位置へ要素を効率的に挿入、削除することを可能にしています。より複雑なリンクリストではリンクをさらに追加することで、任意の要素の参照から要素を効率的に挿入、削除することを可能にしています。リンクリストの欠点はアクセスタイムが線形である(そして、パイプライン処理が難しい)ことです。ランダムアクセスのような高速なアクセスは実現不可能です。配列の方がリンクリストと比較して参照の局所性が優れています。 |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## 基本操作の擬似コード |
| 8 | + |
| 9 | +### 挿入 |
| 10 | + |
| 11 | +```text |
| 12 | +Add(value) |
| 13 | + Pre: value is the value to add to the list |
| 14 | + Post: value has been placed at the tail of the list |
| 15 | + n ← node(value) |
| 16 | + if head = ø |
| 17 | + head ← n |
| 18 | + tail ← n |
| 19 | + else |
| 20 | + tail.next ← n |
| 21 | + tail ← n |
| 22 | + end if |
| 23 | +end Add |
| 24 | +``` |
| 25 | + |
| 26 | +```text |
| 27 | +Prepend(value) |
| 28 | + Pre: value is the value to add to the list |
| 29 | + Post: value has been placed at the head of the list |
| 30 | + n ← node(value) |
| 31 | + n.next ← head |
| 32 | + head ← n |
| 33 | + if tail = ø |
| 34 | + tail ← n |
| 35 | + end |
| 36 | +end Prepend |
| 37 | +``` |
| 38 | + |
| 39 | +### 検索 |
| 40 | + |
| 41 | +```text |
| 42 | +Contains(head, value) |
| 43 | + Pre: head is the head node in the list |
| 44 | + value is the value to search for |
| 45 | + Post: the item is either in the linked list, true; otherwise false |
| 46 | + n ← head |
| 47 | + while n != ø and n.value != value |
| 48 | + n ← n.next |
| 49 | + end while |
| 50 | + if n = ø |
| 51 | + return false |
| 52 | + end if |
| 53 | + return true |
| 54 | +end Contains |
| 55 | +``` |
| 56 | + |
| 57 | +### 削除 |
| 58 | + |
| 59 | +```text |
| 60 | +Remove(head, value) |
| 61 | + Pre: head is the head node in the list |
| 62 | + value is the value to remove from the list |
| 63 | + Post: value is removed from the list, true, otherwise false |
| 64 | + if head = ø |
| 65 | + return false |
| 66 | + end if |
| 67 | + n ← head |
| 68 | + if n.value = value |
| 69 | + if head = tail |
| 70 | + head ← ø |
| 71 | + tail ← ø |
| 72 | + else |
| 73 | + head ← head.next |
| 74 | + end if |
| 75 | + return true |
| 76 | + end if |
| 77 | + while n.next != ø and n.next.value != value |
| 78 | + n ← n.next |
| 79 | + end while |
| 80 | + if n.next != ø |
| 81 | + if n.next = tail |
| 82 | + tail ← n |
| 83 | + end if |
| 84 | + n.next ← n.next.next |
| 85 | + return true |
| 86 | + end if |
| 87 | + return false |
| 88 | +end Remove |
| 89 | +``` |
| 90 | + |
| 91 | +### トラバース |
| 92 | + |
| 93 | +```text |
| 94 | +Traverse(head) |
| 95 | + Pre: head is the head node in the list |
| 96 | + Post: the items in the list have been traversed |
| 97 | + n ← head |
| 98 | + while n != ø |
| 99 | + yield n.value |
| 100 | + n ← n.next |
| 101 | + end while |
| 102 | +end Traverse |
| 103 | +``` |
| 104 | + |
| 105 | +### 逆トラバース |
| 106 | + |
| 107 | +```text |
| 108 | +ReverseTraversal(head, tail) |
| 109 | + Pre: head and tail belong to the same list |
| 110 | + Post: the items in the list have been traversed in reverse order |
| 111 | + if tail != ø |
| 112 | + curr ← tail |
| 113 | + while curr != head |
| 114 | + prev ← head |
| 115 | + while prev.next != curr |
| 116 | + prev ← prev.next |
| 117 | + end while |
| 118 | + yield curr.value |
| 119 | + curr ← prev |
| 120 | + end while |
| 121 | + yield curr.value |
| 122 | + end if |
| 123 | +end ReverseTraversal |
| 124 | +``` |
| 125 | + |
| 126 | +## 計算量 |
| 127 | + |
| 128 | +### 時間計算量 |
| 129 | + |
| 130 | +| Access | Search | Insertion | Deletion | |
| 131 | +| :-------: | :-------: | :-------: | :-------: | |
| 132 | +| O(n) | O(n) | O(1) | O(n) | |
| 133 | + |
| 134 | +### 空間計算量 |
| 135 | + |
| 136 | +O(n) |
| 137 | + |
| 138 | +## 参考 |
| 139 | + |
| 140 | +- [Wikipedia](https://en.wikipedia.org/wiki/Linked_list) |
| 141 | +- [YouTube](https://www.youtube.com/watch?v=njTh_OwMljA&index=2&t=1s&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) |
0 commit comments