in Programming in C
342 views
0 votes
0 votes

in Programming in C
342 views

1 comment

Yeah C is the correct answer.

The process goes like this:-

1. Dequeue upto $n-1$ element from Q1 and Enqueu them in Q2 simultaneously. So

$D = n-1 , E =n -1$

2. Dequeue the top of the stack means $ D = D +1$

3. Now transfer all elements i.e. $n-1$ elements from Q2 to Q1. Means

$D = D + n-1, E = E + n-1$

So in total ,

$D = n-1 + 1+n-1 = 2n-1$

$E = n+1+n-1=2n-2$
0
0

1 Answer

1 vote
1 vote
Pop operation means, we need to pop out the top element from the Stack.

If we store the same element in the queue that we have in stack, the top element of stack will be the last element in the Queue.

 

Suppose there are 2 Queues, Q1 and Q2.

 

Q1 has 'n' elements, now we have to perform pop operation. It means we need the last element from the Queue.

So, we'll dequeue every element except the last element from Q1 and enqueue in Q2.

This Step will Take

#Dequeue= n-1 step

#Enqueue= n-1 step

 

Now Dequeue from Q1(this will be the Pop operation), it'll take 1 step. Total number of Dequeue step is increased by 1.

#Dequeue= n-1 step + 1 step = n step

 

Now, we'll also have to restore the (n-1) element from Q1 to Q2,

So, we'll dequeue every element from Q2 and enqueue in Q1. As there are n-1 element so, n-1 dequeue and n-1 enqueue

 

Total #Dequeue= n step + n-1 step = 2n -1

Total #Enqueue= n-1 step + n-1 step = 2n - 2

Related questions