A queue is FIFO
- A queue is First In, First Out (FIFO).
- The first item you add is the first one to leave.
- Think of a line of people: the person at the front is served first.
キューはFIFOである
- キュー は First In, First Out (FIFO) です。
- 最初に追加したアイテムが最初に退去します。
- 行列を想像してください:先頭にいる人が最初にサービスを受けます。
enqueue and dequeue with a list
- We can build a queue from a Python list.
- enqueue =
queue.append(x)— add to the back. - dequeue =
queue.pop(0)— remove and return the front.
リストを用いたenqueueとdequeue
- Pythonのリストを使ってキューを構築できます。
- enqueue =
queue.append(x)— 後ろに追加します。 - dequeue =
queue.pop(0)— 先頭を削除して返します。
queue = []
queue.append("first")
queue.append("second")
print(queue.pop(0))
print(queue)
front and empty
- Look at the front without removing it:
queue[0]. - A queue is empty when
len(queue) == 0. - Check it is not empty before you dequeue.
frontとempty
- 削除せずに先頭を確認する:
queue[0]。 len(queue) == 0のとき、キューは空です。- dequeueする前に空でないことを確認します。
queue = [10, 20, 30]
print(queue[0]) # the front
print(len(queue) == 0) # is it empty?
A note on speed
pop(0)has to shift every other item forward by one place.- For a big queue that is slow, so it uses more time.
- Real systems use a circular buffer with front and rear pointers instead.
速度に関する注記
pop(0)は他のすべてのアイテムを1つずらす必要があります。- 大きなキューではこれが遅くなるため、より多くの時間がかかります。
- 実際のシステムでは、frontとrearポインタを持つ循環バッファを使用します。
In Cambridge pseudocode
- The exam builds a queue from an array with a
frontand arearpointer.
Cambridge擬似コードにおける表現
- 試験では、配列と
frontとrearポインタを使ってキューを構築します。
DECLARE queue : ARRAY[1:10] OF INTEGER
DECLARE front, rear : INTEGER
front ← 1
rear ← 0 // empty
// enqueue value
rear ← rear + 1
queue[rear] ← value
// dequeue into value
value ← queue[front]
front ← front + 1
Common mistakes
- A queue is first-in, first-out: join the back, leave from the front.
- Check it is not empty before you dequeue.
よくあるミス
- キューは先加入・先除去です:後ろにjoinし、先頭からleaveします。
- dequeueする前に空でないことを確認します。
Now you try
- Use a list as your queue (
appendto enqueue,pop(0)to dequeue). - Press Check answer to test your code.
あなたも試してみよう
- リストをキューとして使用します(
appendでenqueue、pop(0)でdequeue)。 - 回答を確認 を押してコードを試してください。
A queue is FIFO
A queue adds at the back and removes at the front — first in, first out.
Start with an empty queue. Enqueue "a", then "b", then "c". Then dequeue once, storing the removed value in first. (first should be "a" and the queue should be ["b", "c"].)
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Enqueue 1, 2, 3 onto a queue, then dequeue them all into result. Unlike a stack, a queue keeps the same order. (result should be [1, 2, 3].)
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write serve(queue) that dequeues the front item: remove it from the list and return it. The caller's list should get shorter.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write front(queue) that returns the front item without removing it, so the queue is unchanged. If the queue is empty, return None.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。