Question: Problem 3 ( Stacks and Queues, 1 0 % ) It is possible to implement a queue using two stacks stack 1 and stack 2
Problem Stacks and Queues,
It is possible to implement a queue using two stacks stack and stack by implementing the functions in the following manner:
enqueue x: push x on stack
dequeue : if stack is not empty, then pop from it Otherwise, pop the entire contents of stack pushing each one onto stack Now pop from stack
As always, explain your answers thoroughly.
Part a
Starting with an empty queue, indicate the contents of stack and stack after EACH of the following operations have occurred. Clearly indicate the top and bottom of each stack:
enqueue
enqueue
dequeue
enqueue
enqueue
dequeue
enqueue
enqueue
Part b
What is the worstcase runtime of enqueuex and dequeue assuming stack.push and stack.pop run in O
Part c
What is the amortized runtime per function call ie the worstpossible average runtime if you make n calls to some sequence of enqueue and dequeue Note that dequeue can only be analyzed jointly with enqueue.
Part d
Suppose the underlying stack structure is poorly implemented, with pushx taking time, and pop taking n time, where n is the number of elements in the stack. Now what is the worstcase runtime of enqueuex and dequeue What is the amortized runtime per function call?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
