Lists, stacks and queues
導入済み
7:45
データ構造とアルゴリズム
This course is about choosing a container by what it costs. The first unit is the yardstick: how cost grows with the size of the input. The next four are the…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| array/əˈreɪ/ | 配列 |
| linked list/lɪŋkt lɪst/ | リンクドリスト |
| node/nəʊd/ | ノード |
| stack/stæk/ | スタック |
| queue/kjuː/ | キュー |
| amortised/əˈmɔːtaɪzd/ | averaged over a long sequence of operations, so rare expensive steps are shared out among many cheap ones |
| LRU cache/ˌel ɑː ˈjuː kæʃ/ | A cache that evicts the least recently used item when full, usually built from a hash map and a doubly linked list. |
Two layouts, different ends
- A Python list is a contiguous array 数组: the address of index $i$ is computed in one step, but inserting at the front moves every element along. A linked list 链表 gives up that jump for cheap splices: each node 节点 holds a pointer to the next one.
- A stack 栈 adds and removes at the same end (last in, first out: LIFO). A queue 队列 adds at one end and removes at the other (first in, first out: FIFO).

Index number i in a contiguous array costs —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The cells sit side by side: the address is computed, not searched for.
Why append is amortised O(1)
- In a dynamic array that doubles when full, the total number of elements copied over $n$ appends is $O(n)$, so the average append cost is constant: amortised 均摊 $O(1)$.
- Front insertion never averages out. Every
insert(0)moves all the elements, so $n$ front inserts move $\Theta(n^2)$ elements, and the count goes up four times when $n$ doubles.
Append at the end of a Python list is —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Proportional growth gives a linear total number of copies over many appends, so the amortized cost is constant.
Traces
- Stack 栈:
push 3, push 7, push 1, thenpop, popreturns $1$ and then $7$: last in, first out. Balanced brackets use the same rule. Scan([]){}and push each opening bracket; each closing bracket must match the top.([)]fails, because)finds[on top. - Queue: enqueue
a, b, c; two dequeues returnaand thenb, andcis at the front.collections.dequeserves both ends in $O(1)$. Callinglist.pop(0)in a loop is the classic $O(n)$ trap.
Two stacks can act as a queue: an inbox and an outbox, with each element handled at most twice. A real deque simply serves both ends itself.
Last in, first out
Step through push 3, push 7, push 1, then two pops. The pops return 1 then 7, and 3 stays at the bottom.
push 3, push 7, push 1, pop, pop returns —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A stack pops in reverse push order: last in, first out.
Counting the copies
- Start a dynamic array at capacity $1$ and double it when full. Sixteen appends cause regrows at the $2$nd, $3$rd, $5$th and $9$th append, which copy $1 + 2 + 4 + 8 = 15$ elements, fewer than $n$. Writes plus copies stay under $2n$, so each append costs $O(1)$ amortised 均摊.
- Grow by a fixed $+10$ instead and the copies add up to about $n^2/20$: quadratic again. The growth factor matters, not the size of the step. Choose a structure by the end that is busy: undo needs a stack, a print queue needs a queue, a sliding window needs a deque.
Capacity starts at 1 and doubles when full. How many element copies do 32 appends cause?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Regrows copy 1 + 2 + 4 + 8 + 16 = 31 elements, still under n.
An editor must undo the most recent change first. The right structure is —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The last change made is the first undone: last in, first out.
Growing a dynamic array by a fixed $10$ extra slots each time it fills still makes append amortised $O(1)$.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A fixed step regrows every $10$ appends and each regrow copies everything, so the copies total about $n^2/20$: quadratic. Only a constant growth factor, such as doubling, makes append amortised $O(1)$.
Two structures, one cache
- A doubly linked list moves or removes a node in $O(1)$ once you hold it; a hash map finds a node in $O(1)$. Together they make an LRU cache LRU缓存 (least recently used): every get moves the item to the front, and a full cache removes the item at the tail.
- Neither structure can do it alone: the list cannot find a key, and the map has no order. Python's
OrderedDictwithmove_to_endis exactly this pair.
A hash map alone gives an LRU cache with O(1) eviction.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It has no order to find the least recently used item; the linked list supplies it.
Worked example: a ring-buffer queue
- Store a queue 队列 in an array of capacity 8, with
headandtailindices taken mod 8. From an empty queue athead = tail = 6, enqueue three items: they go into slots 6, 7 and 0. - Now
tail$= (6 + 3) \bmod 8 = \mathbf{1}$, and the size is $(1 - 6) \bmod 8 = 3$. Both ends stay $O(1)$ with no shifting.dequeblocks and network buffers work this way.
Ring buffer of capacity 8, empty with head = tail = 6; enqueue 3 items. New tail index?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
$(6 + 3) \bmod 8 = 1$.
Capacity 8, head 6, tail 1: number of items?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
$(1 - 6) \bmod 8 = 3$.
Marks that slip away
list.pop(0)in a loop. Each call moves every element, so emptying $n$ items costs $\Theta(n^2)$. Usecollections.deque.- Calling every append $O(1)$. A regrow copies everything; only the average over many appends is constant.
- Mixing up the ends. Push 3, 7, 1: a stack 栈 gives back 1 first, a queue gives back 3.
Recap
| Operation | Array | Linked list 链表 |
|---|---|---|
| Index lookup | $O(1)$ | $O(n)$ |
| Insert at the front | $O(n)$ | $O(1)$ |
| Append at the end | $O(1)$ amortised | $O(1)$ with a tail pointer |
| Splice in a held node | $O(n)$ | $O(1)$ |
- Doubling makes append amortised $O(1)$: $15$ copies for $16$ appends.
- Stack is LIFO, queue is FIFO;
collections.dequeserves both ends in $O(1)$.
何が問題でしたか?この内容はサイトの運営チームだけが確認します。