617 views
0 0 votes
We want to reverse elements of stack using a queue.Suppose stack operations,push and pop take 1 unit of time each and queue operations insert and delete take 2 units of time each.Assume n elements are there in the stack.The minimum time to reverse the stack is

(A) 4n units

(B) 6n units

(C) 8n units

(D) 9n units

1 Answer

3 3 votes
(B) 6n units is the answer.

Lets take an example the stack is having n elements present in it. Now to reverse them using queue we have to perform two steps n times.

1. pop the element from the stack and insert it to the queue . This will take n+2n time units respectively.

2.delete the element from the queue and push it into stack.This will take 2n+n time units respectively.

so the total time = 3n+3n=6n
Position:
Show:

Related questions

10 10 votes
2 2 answers
501
501 views
GO Classes asked Jul 27
501 views
Given a stack $S$ with $5$ elements from top to bottom as:$2, 4, 6, 8, 10$and an empty queue $Q$.First, remove the elements one by one from $S$ and insert them into $Q$.T...
8 8 votes
2 2 answers
626
626 views
GO Classes asked Jul 27
626 views
Which of the following statements are true?$\text{S1.}$ Stack operations $\texttt{push}$, $\texttt{pop}$, and $\texttt{isEmpty}$ can be worst-case $O(1)$ for a linked-lis...
7 7 votes
1 1 answer
364
364 views
GO Classes asked Jul 10
364 views
Assume there are $n$ elements in the data structure. Consider the following statements:$\text{S1}:$ A stack can be implemented using a linked list such that each individu...
6 6 votes
3 3 answers
361
361 views
GO Classes asked Jul 8
361 views
Suppose an intermixed sequence of stack push and pop operations is performed. The push operations push the integers $0$ through $9$ in order. The pop operations print the...