← 回題庫

卷別

資料結構及演算法

選擇

國立政治大學 2026 · 考試 · 第 2 題

(5%) A queue is implemented using two stacks, SinS_{in} and SoutS_{out}. The implementation ensures the FIFO property by defining the operations as follows:

  • Enqueue: The new element is simply pushed onto SinS_{in}.
  • Dequeue:
    • If SoutS_{out} is not empty, the top element is popped from SoutS_{out}.
    • If SoutS_{out} is empty, all elements are first transferred from SinS_{in} to SoutS_{out} (by repeatedly popping from SinS_{in} and pushing onto SoutS_{out}), and then the top element is popped from SoutS_{out}.

What is the amortized time complexity of the dequeue operation?

  • (A)
    O(1)O(1)
  • (B)
    O(log⁡n)O(\log n)
  • (C)
    O(n)O(n)
  • (D)
    O(nlog⁡n)O(n\log n)

標準解析

AI 已複查