先入れ先出し(First-In First-Out, FIFO)処理を行うのに適したキューと呼ばれるデータ構造に対して “8”,“1”,“6”,“3”の順に値を格納してから,取出しを続けて2回行った。2回目の取出しで得られる値はどれか。
平成30年度 春期 ITパスポート試験 問96
This question's explanation and your answers so far.
問 96
- ア
正解 A queue is a First-In First-Out (FIFO) data structure, in which the value stored earlier is taken out earlier. Here the values are stored in the order “8”→“1”→“6”→“3”, so the take-out order is the same as the storage order: “8”→“1”→“6”→“3”. The first take-out yields “8”, the value stored first, and the second take-out yields “1”, the value stored next. Therefore the value obtained by the second take-out is “1”, and the correct answer is ア.
Incorrectイ
“3” is the value stored last (fourth). In FIFO the value stored last is taken out last, so it is taken out on the fourth operation, not on the second.
Incorrectウ
“6” is the value stored third, so it is also taken out on the third operation, not the value obtained by the second take-out.
Incorrectエ
“8” is the value stored first, and in FIFO it is taken out first. Therefore it is the value obtained by the first take-out, not the second.
- A queue is a First-In First-Out (FIFO) structure: values are taken out in the same order they were stored, and the later a value is stored, the later it is taken out.
- Grasp the contrast with a stack, which is Last-In First-Out (LIFO). With a stack the take-outs are in reverse order, so after “3” the second take-out would be “6”; a queue instead takes out values in the order they were stored, which is the difference.