| English | 意味 |
|---|---|
| overflow/ˌəʊvəˈfləʊ/ | オーバーフロー |
| underflow/ˌʌndəˈfləʊ/ | アンダーフロー |
| circular array/ˈsɜːkjʊlə əˈreɪ/ | 巡回配列 |
| free list/friː lɪst/ | フリーリスト |
There is no such thing as a stack in memory
- Open a computer and look for the stack. You will not find one. Memory is one enormous array of numbered cells, and that is all there is.
- Every stack, every queue, every linked list is that array plus two or three integer variables that remember where things are. Push is "add one to a number and store"; dequeue is "read a cell and add one to a different number".
- The whole of this lesson is bookkeeping: which pointers, which checks, and what happens at the edges.
- The exam asks you to describe the declarations, walk the pointers through a few operations, and say why the checks are there.
A stack in an array
- Hold the items in
Stack[1:MaxSize]with an integerTop, 0 when the stack is empty. - Push(x): if
Top = MaxSizethe stack is full, an overflow 溢出; otherwiseTop ← Top + 1andStack[Top] ← x. - Pop(): if
Top = 0the stack is empty, an underflow 下溢; otherwise returnStack[Top]andTop ← Top − 1.

The array never moves; only Top does
すでに満杯のスタックにアイテムをプッシュすると、スタック______が発生する。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
Top = MaxSize の状態でプッシュすることはオーバーフローであり、Top = 0 の状態からポップすることはアンダーフローである。
各配列スタックの条件と、それが意味するものを対応させよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
Topはアイテム数をカウントする:0 = 空、MaxSize = 満杯。2つのエラーケースはアンダーフローとオーバーフローである。
Worked example: declare and initialise the stack
- Describe the declarations and initialisation needed to implement a stack of up to 50 integers using an array. [5]
- An array of 50 elements of type
INTEGER,DECLARE Stack : ARRAY[1:50] OF INTEGER, to hold the items. - A constant or variable
MaxSizeset to 50, so that push can test for full. - An
INTEGERtop-of-stack pointer,Top, initialised to 0 to show the stack is empty; pop tests it for underflow, and push compares it withMaxSizefor overflow.
配列ベースのスタックの宣言と初期化に含めるべきものはどれか。すべて選びよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
スタックにはTopという1つのポインタ就够了。フロントポインタはキューのものである。
A queue in a plain array
- Two pointers:
Frontfor the next item to leave,Rearfor the next free space. Enqueue stores atRearand moves it on; dequeue reads atFrontand moves it on. - Both pointers only ever move forward, so after a few operations they march off the end of the array while the cells at the start sit empty and unusable.
- The fix is to let the pointers wrap around.
The circular array
- A circular array 循环数组 wraps a pointer back to the first cell when it passes the last: with 1-based indices,
Rear ← (Rear MOD MaxSize) + 1. - Enqueue(x): check not full;
Rear ← (Rear MOD MaxSize) + 1;Queue[Rear] ← x. Dequeue(): check not empty; returnQueue[Front];Front ← (Front MOD MaxSize) + 1. - Keep a separate count: when the queue is completely full and when it is completely empty the two pointers are in the same relative position, so the pointers alone cannot tell the two apart.

After the last cell comes the first cell
配列によるADTの実装
FIFO
キューは先入れ先出しであり、最後尾でエンキューし、先頭でデキューする。
なぜキューに環状配列を使うのか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
線形キューではFrontが進むにつれて先頭のセルが廃棄されるが、MODによる巻き返しで再利用できる。
環状キューはMODを用いることで、front/rearポインタが巻き返って、配列の先頭で解放されたセルを再利用する。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
(pointer MOD MaxSize) + 1 でインデックスが最初のセルに戻り、線形キューがFrontが通過したセルを無効化して使用しない状態を防ぐ。
Worked example: walk the pointers
- With
MaxSize = 6: ifRear = 5, then(5 MOD 6) + 1 = 6, so the next item goes in cell 6. IfRear = 6, then(6 MOD 6) + 1 = 1: the pointer wraps to cell 1. - A circular queue is held in an array of size 5, indices 0 to 4, with
Front = 3,Rear = 3and one item stored. Two items are added, then two removed. With 0-based indices each move is(pointer + 1) MOD 5. - Adding twice moves
Rear: 3 → 4, then 4 → 0, because (4 + 1) MOD 5 = 0. Removing twice movesFront: 3 → 4 → 0. One item remains, at index 0, and the queue reused the cells freed at the start of the array.
環状キューがセル1〜6を使用し Rear = 6 のとき、Rear ← (Rear MOD 6) + 1 を実行した後、次のアイテムはどこに入るか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
6 MOD 6 = 0、これに1を加えると1になる。ポインタは配列の先頭へ巻き返る。
Worked example: the enqueue algorithm in words
- Describe the algorithm for adding an item to a circular queue. [4]
- If the count equals the size, report that the queue is full and stop.
- Otherwise add one to the rear pointer; if it is now past the last index, set it to the first index.
- Store the item at the rear pointer and add one to the count.
環状キューに追加する手順を正しい順序で並べ替えてください。
難易度 3/5 · 200 コイン + 今日初回完了時に小ボーナス付与
確認→移動→巻き返し→格納→カウント。巻き返しが配列を環状にする要因である。
A linked list in an array
- Use an array of node records, each with a
Nextindex;-1marks the end. AHeadindex marks the first node,-1if the list is empty. - The unused slots are chained into a free list 空闲列表 from
FreeListHead, exactly as the data list chains its used ones.
TYPE TNode
DECLARE Value : INTEGER
DECLARE Next : INTEGER // index of the next node, or -1
ENDTYPE
DECLARE Nodes : ARRAY[1:MaxSize] OF TNode
DECLARE Head : INTEGER // -1 when empty
DECLARE FreeListHead : INTEGER // first unused slot
- Insert: take the slot at
FreeListHead, set itsValueandNext, then rewire the previous node'sNextorHead. Delete: unlink the node and return its slot to the front of the free list.

Two lists share one array: the data list and the free list
配列ベースのリンクドリストにおいて、フリーリストは:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
フリーリストは予備スロットをリンクするため、挿入時に一つを取得し、削除時に一つを戻すことができる——これは「空白の別のリンクドリスト」のような働きをする。
Worked example: insert into the array-held list
DataandPointerarrays hold the list 1 → 3 → 4, withStart = 1; index 1 holdsD40, index 3 holdsD32, index 4 holdsD11with a null pointer. The free list starts at index 2 and continues 2 → 5. InsertD6betweenD32andD11.- Take the first free node, index 2, and set
FreeStartto its pointer, 5. StoreD6inData[2]. - Set
Pointer[2]to the valuePointer[3]held, which is 4. Then setPointer[3]to 2. - The list now reads 1 → 3 → 2 → 4 and the free list is 5 → null. The implementation, if asked: an array for the data, a parallel array (or record field) for the pointers, a start pointer, and a free-list pointer.
解例において、D6が挿入された後、フリーリストの始点はインデックス ____ である。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
インデックス2はフリーリストから取得されたため、FreeStartはインデックス2が指していた先(インデックス5)へ移動する。
リストへの挿入時、新しいノードのポインタを設定する前に、前のノードのポインタを変更すべきである。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
まず新しいノードのポインタを古い次のノードに設定する。前のノードを先にリワイヤリングすると、リスト残りの部分のアドレスを失ってしまう。
Marks that slip away
- The checks come first: full before push or enqueue, empty before pop or dequeue. Describe them; they are marks.
- The wrap formula depends on the indices:
(Rear MOD MaxSize) + 1for 1-based,(Rear + 1) MOD Sizefor 0-based. Match the question's bounds. - The count is what tells a full circular queue from an empty one. Pointers alone cannot.
- Set the new node's
Nextbefore rewiring the previous node, and return a deleted node's slot to the free list, or the array slowly fills with unreachable cells.
You've got it
- stack in an array:
Stack[1:MaxSize]and aToppointer starting at 0;Top = MaxSizeis overflow,Top = 0is underflow - circular queue:
FrontandRearwrap withMOD; a separate count distinguishes full from empty - linked list in an array: node records with a
Nextindex, aHead, and a free list chaining the spare slots - every operation is check, then pointer arithmetic, then store or read; the ADT's behaviour is unchanged by how it is stored