Implement Stack by using Queue (with Example)

preview_player
Показать описание
This lecture explains how to implement STACK by using two Queues by using a very simple example. I have also discussed 2 methods to do it and have explained the circumstances of using each method.
If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

ALGO:-
1. By making PUSH costly

push(s, x) // x is the element to be pushed and s is stack
1) Enqueue x to q2
2) One by one dequeue everything from q1 and enqueue to q2.
3) Swap the names of q1 and q2
// Swapping of names is done to avoid one more movement of all elements
// from q2 to q1.

pop(s)
1) Dequeue an item from q1 and return it.

2. By making POP costly

push(s, x)
1) Enqueue x to q1 (assuming size of q1 is unlimited).

pop(s)
1) One by one dequeue everything except the last element from q1 and enqueue to q2.
2) Dequeue the last item of q1, the dequeued item is result, store it.
3) Swap the names of q1 and q2
4) Return the item stored in step 2.
// Swapping of names is done to avoid one more movement of all elements
// from q2 to q1.

Рекомендации по теме
Комментарии
Автор

Now whenever I search for any problem and I get ur video in search suggestion, without even looking at the video I get assured that this question will be now crystal clear. ur way of explaining things is unmatchable..

PoojaGupta-ryoj
Автор

I'll surely donate some money once I got placement.
your videos are just awesome.

nimishgupta
Автор

You've managed to explain this better than what Sandeep Jain in his paid data structures course. Kudos!

tanmay
Автор

Kya gazab pdhate ho bhai 7000 ke course me smjh nhi aya tumne to gazab hi krdia

abhigyansharma
Автор

Nicely explained. No other channel was able to explain this clearly.

bhargavreddy
Автор

U r far better then those thousand paid course
Thnks u

vishalvikram
Автор

This is clearly explained, Thank you!

sherryz
Автор

Thanks for making this very helpful sir

pralayghosh
Автор

respected sir, in the video u have told that in costly push operation we first need to enqueue in Q1 and thn dequeue it from there and thn enqueue it in in algo u have directly enqueue it in also in the same algo on the very next line u wrote to dequeue from q1...bt we havent entered anything in q1....plz explain sir....also i have reffered the code there also same operations are taking place

shreeyanshisrivastava
Автор

Why do we even need the second queue? Wouldn't one queue suffice? Like so: every time we want to push, first we push the given element in the queue and then we can dequeue the existing elements(except the rear, which we just inserted in the queue) from the queue and then enqueue them again in the same queue.

mindsauce
Автор

one doubt sir, how to know the last element of queue in 2 nd approach

govardhanerramchetti
Автор

Please clear concept on dequeue, because during dequeue, front never gets updated, the elements come one address nearer!

sandipchatterjee
Автор

How to contact for you. I come to VIỆT NAM

QuynhNhu-guql
Автор

Can we do the same using recursive approach with single queue?

ss
Автор

Just use one queue and reverse it one by one for each push

sakibmalik
Автор

Why we are using 2 queues! Hard to understand this video. Please explain clearly

manvirkaur
Автор

Did anybody notice how many times he said 'ok'😂

rishikshrivastava
Автор

Sir, kindly make video on solution for google codejam alien rhyme solution... Kindly sir... 🙏🙏🙏🙏🙏🙏

dhayalanm
Автор

Alright, now please explain how to get both push and pop in O(1)

thomasboer
Автор

I guess number of pop operations can never be more than number of push so the last logic might not be true.

gannuatharvan