Question: 3 , 2 Stacks = 1 FIFO Queue We implement a first - in - first - out ( FIFO ) queue using two stacks
Stacks FIFO Queue
We implement a firstinfirstout FIFO queue using two stacks in the following awkward manner. We
can visualize the two stacks backtoback:
Stack # is used for enqueuing. We push an item on Stack # to enqueue.
This takes actual time.
Stack # is used for dequeuing. We pop an item off Stack # to dequeue.
This takes actual time.
If the Stack # is empty when we want to dequeue, then we copy everything from Stack # to Stack
# by popping each item from Stack # and pushing the item on Stack # This takes actual
time, where is the number of items moved from Stack # to Stack #
Devise an amortized analysis using the accounting method to show that the enqueue and dequeue operations
are amortized time.
Questions:
When do you add credits to the data structure? How many credits do you add?
When do you remove credits from the data structure? How many credits do you remove?
Argue that enqueue takes amortized time in your analysis.
Argue that dequeue takes amortized time in your analysis.
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
