Implement Queue using two Stack & Implement Stack using two Queue | DSA-One Course #51

preview_player
Показать описание
Hey guys, In this video, We're going to solve two very important problems on Stacks and Queues.

0:00 Implement Queue using Two Stacks
5:08 Implement Stack using Two Queues

🥳 Join our Telegram Community:

🚀 Follow me on:

💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!

Hashtags:
#anujbhaiya #dsaone

Tags:

implement queue using stack
implement stack using queue
stack using queue
queue using two stacks
queue using stack
stack using two queues
anuj bhaiya
implement queue using stacks
queue
implement stack using queues
stack and queue in data structure
stack and queue
stacks
stack
queue using 2 stacks
queue using two stack
stack using two queue
232. implement queue using stacks
implementation of queue using stack
stacks and queues
stack using 2 queues
225. implement stack using queues
queue data structure
queue in java
queue using stacks
implement stack using two queue
stack implementation
stacks and queues in data structure
stack queue
anuj bhai
queue in data structure
queue using array
stack and queue playlist
queue c++
stack implementation using queue
225. implement stack using queues java
anuj bhaiya java
double ended queue
dsa
how to implement stack using queue
implement queue insertion and deletion operations using two stacks.
implementation of stack
implementation of stack using queue
queue implementation
queue using two stacks gfg
queues
stack algorithm
stack using array
stacks and queue playlist
stacks and queues level 1
structured query language
array implementation of stack
blocking queue
circular queue
dsa stack
implement queue using stack leetcode
implement stack using 2 queue
implement stack using 2 queues
implement stack using linked list
implement stack using queue leetcode
implement two stacks in an array
implementation of stack using linked list
implementing queue using stack
Рекомендации по теме
Комментарии
Автор

Anuj Bhaiya please, make a series of videos related to Java which contains all the topics which mostly asked in interviews.
And Bhaiya your DSA series is superb. No need to buy any DSA course form any where I think it's sufficient to brush up all the DSA concepts.

tariquekhan_
Автор

Stack using queue me ek he queue se bhe ho sakta hai na kaam, pehele normally push krdo queue me then size - 1 tak loop chala kar dequeue and then enque the same element

gouravchouhan
Автор

Please make a 45day roadmap of DSA for working professional. To crack companies like Microsoft linkedin level companies

tech_wizard
Автор

In stack using queues code implementation, offer and poll should be used instead of push and pop.

nancyagarwal
Автор

during insertion in stack implementation using 2 queues the process is following stack rule but not queue rule whenn one element is pushed in q1 it should be store after the previous pushed element.

allhdmoviescene
Автор

very well explained bhaiya ..i searched whole youtube but you cleared my queries in 1 video

vinayakupadhyay
Автор

class Queue_double_Stack:

def __init__(self):

self.stack_1 = []
self.stack_2 = []


def enqueue(self, item):

self.stack_1.append(item)

def dequeue(self):

if len(self.stack_2) == 0:
if len(self.stack_1) == 0:
return "Empty stack"

while len(self.stack_1) > 0:
remov = self.stack_1.pop()
self.stack_2.append(remov)

return self.stack_2.pop()

# IN PYTHON

shankhadeepghosh
Автор

Please make a video on implementation of stack using two queue
Push(1)
Push(2)
Push(3)
Pop()
Push(6)

softmedun
Автор

Bhaiya dsa ke alava baki kya-kya cover karna or kaha se. Kuch guide kar do please🙏

sharwanjakhar
Автор

Thank you Anuj Bhaiya, This video cleared my doubt.

videos-ebrq
Автор

Leetcode Solution -
class MyQueue {
Stack<Integer> s1 = new Stack();
Stack<Integer> s2 = new Stack();

public void push(int x) {
s1.push(x);
}

public int pop() {
peek();
return s2.pop();
}

public int peek() {
if(s2.empty()){
while(!s1.isEmpty()){
s2.push(s1.pop());
}
}
return s2.peek();
}

public boolean empty() {
if(s1.isEmpty() && s2.isEmpty()){
return true;
}
else{ return false;}
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/


class MyStack {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();


public void push(int x) {
while(!q1.isEmpty()){
q2.add(q1.remove());
}
q1.add(x);
while(!q2.isEmpty()){
q1.add(q2.remove());
}
}

public int pop() {
return q1.remove();
}

public int top() {
return q1.peek();
}

public boolean empty() {
if(q1.isEmpty() && q2.isEmpty()){
return true;
}
else{ return false;}
}
}

/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

pandeyshashidhar
Автор

there should be a if statment in this for stack and queue from which elements are gonna pop are empty then return -1

tejasukalkar
Автор

in queue using two stack, when poping the element we need to add check if stack is empty then we need to return -1 but great explanation by you bhaiya

ujjwalrai
Автор

This course is very helpful for me thanks bhaiya ❤️❤️

rahulchouhan
Автор

It was great explanation, I am wondering if there can be any use-case for such implementation, I saw similar question in one of CS exam

gopalchavan
Автор

thank you so much for this great course

arifsaleem-
Автор

if you write s1.pop() .It will just delete element in stack . I dont think if i write s2.push(s1.pop()) it will push element into s2. I think it should be done using top and then pop.pop(): Removes the top element from the stack. It does not return the element.

factsclubu
Автор

bhaiya pls code ko code editor par karke dikhaya karo usse extra help ho jati hai aise kabhi kabhi implement karne mai problem hoti hai

therealartist
Автор

It was so helpful sir!!!Thanks a lot:)

NandanaM-vbps
Автор

Better than popular paid Online plaforms. Dont want to mention name of these platforms but trust me its far bettrt than paid courses

Abhishek-jiqj