The Gateway to Computer Science Excellence
+1 vote
Show how to implement a stack using two queues. Analyze the running time of the stack operations.
in Algorithms by Boss (41.9k points) | 69 views

1 Answer

+1 vote

we take two QUEUE say A and B we push all the element in A.i.e A={1,2,3,4,5} then here first element is 1=head and 5=tail .we know that stack follow LIFO dequeue n-1 element and place into QUEUE B then there is only one element in A so we can pop it .for push operation it will take o(1) time for pop operation it will take o(n) time .NOTE- here  o(n) popoperation is for single element. 

by Active (2.8k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,466 answers
100,309 users