You are tasked with implementing a queue data structure using two stacks. Your implementation should support all the essential operations of a queue: add, poll, peek.
add(x): Adds element x to the back of the queue. This operation does not return a value.poll(): Removes and returns the element from the front of the queue.peek(): Returns the element at the front of the queue without removing it.q = QueueUsingStacks() q.add(1) q.peek() q.poll()
[1, 1]
Initial queue is empty. add(1) adds 1. The queue is [1]. peek() returns 1 without removing it. poll() removes and returns 1. The queue is now empty.
q = QueueUsingStacks() q.add(1) q.add(2) q.poll() q.add(3) q.poll() q.poll()
[1, 2, 3]
Initial queue is empty. add(1), add(2), and add(3) add elements to the queue. The queue is [1, 2, 3]. The first poll() removes and returns 1. The second poll() removes and returns 2. The third poll() removes and returns 3. The queue is now empty.
q = QueueUsingStacks() q.add(1) q.add(2) q.poll() q.add(3) q.poll() q.poll()
[1, 2, 3]
Initial queue is empty. add(1) and add(2) add elements to the queue. The queue is [1, 2]. The first poll() removes and returns 1. add(3) adds 3 to the back. The queue is now [2, 3]. The second poll() removes and returns 2. The third poll() removes and returns 3. The queue is now empty.
q = QueueUsingStacks() q.add(1) q.peek() q.poll()
[1, 1]