edited by
511 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

edited by

1 Answer

Best answer
1 votes
1 votes
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

Related questions

0 votes
0 votes
2 answers
1
Na462 asked Jan 16, 2019
753 views
void PrintValue(int n) { if (n < 0) return; else { printf(n); printValue( n); printValue(n ); printf(n); } }Output for Print(5) ?
2 votes
2 votes
4 answers
2
srestha asked Apr 24, 2019
3,813 views
Which of the following data structure is efficient to implement priority queue such as insertion ,deletion, searching?A)Linked ListB)HeapC)Sorted ArrayD)Unsorted ArrayHow...
2 votes
2 votes
1 answer
3
0 votes
0 votes
1 answer
4
Markzuck asked Jan 10, 2019
522 views
Please explained detialed execution of this code, I am not getting how int stored in char, like we can interchange using ASCII but still cant store right?