in Programming in C edited by
504 views
2 votes
2 votes

Suppose a queue $Q$ and two stacks $S_{1}$ and $S_{2}$ as given below.

void enqueue(Q,x){
    push(S1,x);
}
void dequeue(Q,x){
    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);
}

The number of push and pop operation needed is represented by $X$ and $Y$, Then $X+Y$ is equal to______________

$Enqueue(4),Enqueue(3),Enqueue(2),Dequeue, Enqueue(6),Dequeue,Dequeue, Dequeue,Enqueue(5)$


Please tell value of X and Y are u getting

in Programming in C edited by
by
504 views

1 Answer

1 vote
1 vote
Best answer
I think the ans. should be 17 because for every enqueue opeation there is only one stack operation (simply push in S1 ) performed .

but in case of dequeue  we required 3 stack operation

1. POP from stack1
2. Push in  stack2
3. Then POP from stack2

Here we have 5 enqueue operation and 4 dequeue opeation

Therefore X=9  ( 5 push for enqueue and 4 push for dequeue) And     Y=8 (2 times POP for dequeue)
selected by

2 Comments

yes, we can say

$4$ elements push in 1st stack

pop from 1st stack.

then push in 2nd stack

and pop from 2nd stack

Total $8$ push and $8$ pop.operation

Now, last element only push in 1st stack.
0
0

 mam after enqueue(6) ....if we are going with this code we are not enable to pop (3) ?? i mean something wrong in the code ..

0
0

Related questions