2,943 views
0 votes
0 votes
Consider a stack implementation supports, in addition to PUSH and POP, an operation REVERSE, which reverses the order of the elements on the stack. Which of the following represents the minimum stack operations required to implement ENQUEUE and DEQUEUE operations of queue data structure respectively?

a) 1 and 3

b) 3 and 1

c) 2 and 2

d) Either a or b

1 Answer

0 votes
0 votes

Answer is either a or b therefore option D is right answer

let already in queue, there are a,b,c,d

inserting e 

How option A is right ?

For enqueue, ====>  push --------------> a,b,c,d,e

For dequeue, ====>  reverse, pop, reverse ------> i) e,d,c,b,a, ii) pop a, then stack look like e,d,c,b iii) b,c,d,e


How option B is right ?

For enqueue, reverse,push,reverse

For dequeue, pop

 

for analyzing this , take empty queue, add a,b,c,d one by one, delete, add e and delete ===> queue now look like as c,d,e where c is front and e is rear

note that my representation, stack contains 1,2,3,4 means 1 is at bottom of the stack and 4 is at top of the stack

enqueue a :- 1) reverse  2) push a ( a )  3) reverse ( a ) ===> after enqueuing stack look like as  a

enqueue b :- 1) reverse ( a )  2) push b ( a,b )  3) reverse ( b,a ) ===> after enqueuing stack look like as  b,a

enqueue c :- 1) reverse ( a,b ) 2) push c ( a,b,c )  3) reverse ( c,b,a ) ===> after enqueuing stack look like as  c,b,a

enqueue d :- 1) reverse ( a,b,c )  2) push d ( a,b,c,d )  3) reverse ( d,c,b,a ) ===> after enqueuing stack look like as  d,c,b,a

dequeue :- 1) pop ===> after dequeuing stack look like as  d,c,b

enqueue e :- 1) reverse ( b,c,d )  2) push e ( b,c,d,e )  3) reverse ( e,d,c,b ) ===> after enqueuing stack look like as  e,d,c,b

dequeue :- 1) pop ===> after dequeuing stack look like as  e,d,c

edited by

Related questions

369
views
3 answers
0 votes
Mrityudoot asked Jan 20
369 views
In a max heap of n elements, the time complexity to find 10th largest element is:a)Θ(n log n)b)Θ(n)c)Θ(1)d)Θ(log n) I personally think it should ... can be anywhere in the heap, all the elements need to be traversed once to decide that.
805
views
1 answers
0 votes
atulcse asked Jan 16, 2022
805 views
Consider the following graph:Find the total number of max-heap possible orderings with elements 12, 10, 1, 5, 7, 9, 8 such that each element is filled in ... of the above tree and element 10 occupies only the left child node of its parent.
2.0k
views
5 answers
3 votes
srestha asked May 22, 2019
1,975 views
Consider the following function height, to which pointer to the root node of a binary tree shown below is passedNote that max(a,b) defined by #define max(a ... a:b.int height(Node *root)The output of the above code will be _________________
808
views
1 answers
1 votes
srestha asked May 22, 2019
808 views
A stack based CPU executes the instruction. Memory location $500$ contain $0X 88$ and memory location $700$ contain $0X37$. The stack pointer is at $0X003F$The ... contain $0XBF$ after execution of instruction.$D)$ Both $a)$ and $c)$