An implementation of a queue Q, using two stacks S1 and S2, is given below:void insert(Q, x) { push (S1, x);} void delete(Q){ if(stack-empty(S2)) then if(stack-empty(S1)) then { print(“Q is empty”); return; } else while (!(stack-empty(S1))){ x=pop(S1); push(S2,x); } x=pop(S2);}Let n insert and m (<=n) delete operations be performed in an arbitrary order on an empty queue Q. Let x and y be the number of push and pop operations performed respectively in the process. Which one of the following is true for all m and n?n+m <= x < 2n and 2m <= y <= n+mn+m <= x < 2n and 2m<= y <= 2n2m <= x < 2n and 2m <= y <= n+m2m <= x <2n and 2m <= y <= 2n
Question
An implementation of a queue Q, using two stacks S1 and S2, is given below:void insert(Q, x) { push (S1, x);} void delete(Q){ if(stack-empty(S2)) then if(stack-empty(S1)) then { print(“Q is empty”); return; } else while (!(stack-empty(S1))){ x=pop(S1); push(S2,x); } x=pop(S2);}Let n insert and m (<=n) delete operations be performed in an arbitrary order on an empty queue Q. Let x and y be the number of push and pop operations performed respectively in the process. Which one of the following is true for all m and n?n+m <= x < 2n and 2m <= y <= n+mn+m <= x < 2n and 2m<= y <= 2n2m <= x < 2n and 2m <= y <= n+m2m <= x <2n and 2m <= y <= 2n
Solution
The correct answer is "n+m <= x < 2n and 2m<= y <= 2n".
Here's why:
- For every insert operation, we perform one push operation on S1. So, for n insert operations, we perform n push operations.
- For every delete operation, if S2 is empty, we pop all elements from S1 and push them into S2. In the worst case, this can happen for every delete operation. So, for m delete operations, we can perform at most n additional push operations and at most n additional pop operations.
- Therefore, the total number of push operations x is at least n+m (for the n insert and m delete operations) and less than 2n (since we can perform at most n additional push operations).
- Similarly, the total number of pop operations y is at least 2m (for the m delete operations and the at most m additional pop operations during the delete operations) and at most 2n (since we can perform at most n additional pop operations).
So, the statement "n+m <= x < 2n and 2m<= y <= 2n" is true for all m and n.
Similar Questions
Implement Stack using Queues
What will be the final elements on the queue and stack if the following sequence of operations are executed? Queue q = new Queue(); Stack s = new Stack(); q.push("a"); s.push("b"); q.push("c"); s.push("d"); s.push( q.pop() ); q.push( s.peek() ); q.push( s.pop() );
Q3(a) Suppose a stack implementation supports an instruction REVERSE, which reverses the order of elements on the stack, in addition to the PUSH and POP instructions. Implement queue function using these three functions and only one stack.
void myfun(Queue *Q){Stack S;// Say it creates an empty stack S while (!isEmpty(Q)) { push(&S, deQueue(Q)); } while (!isEmpty(&S)) { enQueue(Q, pop(&S)); }}What does the myfun do in general?
Identify and describe each operation(s) in a Stack and a Queue.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.