Implement Stack using Queues | Queue in Data Structure & Algorithms | Interview Question Hello World

preview_player
Показать описание
This is the video under the series of DATA STRUCTURE & ALGORITHM in a STACK & QUEUE Playlist. Now we are going to Learn to solve Questions on Queue in Data Structure from Leetcode Implement Stack using Queues.

---------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

*Follow me *

----------------------------------------------------------------------------------------

►Our Playlists on:-

------------------------------------------------------------------------

🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟

✨ Tags ✨
leetcode problems
leetcode problems java
leetcode problems python
leetcode problems that got me tired
leetcode problems c++
leetcode problems and solutions python
leetcode problems playlist
leetcode problems and solutions java
leetcode problems in Hindi
leetcode problems javascript
leetcode problems and solutions
leetcode problems of the day
leetcode problems for beginners
leetcode problems easy
Implement Stack using Queues
Introduction to the queue data structure
Queue playlist Hello world
Queue practice problems
Queue practice problems gfg
leetcode Queue questions
leetcode Queue queue
Queue hello world
Types of Queue in Data structure & algorithms
playlist Queue Hindi
question asked in Google
off-campus placement
Practice Queue data structure
Queue in a data structure in Hindi
Queue Full playlist for Beginners
algorithms
graph
data structure
sorting algorithms
algorithm analysis
gate computer science preparation
programming languages

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

That's the wonderful explanation on the stack & queue. Bhaiya!!

DPCODE
Автор

Most Easiest Way To Understand Thank You SO MucH.

neelsoni
Автор

bhaiya in your example you have taken element in stack 2, 3, 4(down to up) but it should be 4, 3, 2 (down to up) bcz how you have put 4, 3, 2 in q1.

Manish-wwlg
Автор

very to the point explaination sir thank you

apna_Vaibhav
Автор

great video... best channel for dsa problems.

sus_tha_coder
Автор

in question clearly written we have only use push from back and pop of queue ! so you had use front

ankitkumar-rioh
Автор

done without watching video love you bhaiya

samridhrana
Автор

bhaiya can we consider the rear as the top of stack ?

pritishpattnaik
Автор

done by myself in first go using 1 queue, thanks bro improving daily bcs of u:
class MyStack {
public:
queue<int>q1;
MyStack() {

}

void push(int x) {
q1.push(x);
}

int pop() {
int n=q1.size();
for(int i=0;i<n-1;i++)
{
int f=q1.front();
q1.pop();
q1.push(f);
}
int f=q1.front();
q1.pop();
return f;

}

int top() {
return q1.back();
}

bool empty() {
return q1.empty();
}
};

memefulland
Автор

1st method :
class MyStack {
queue<int> queue1;
queue<int> queue2;
// Move all elements except the last one to queue2 --otherwise queue2 will be similar to queue 1 -- so now last elem is remaining in queue1 we can perform desired operation on it and then swap both queues afterwards so -- queue1 does not remain empty
void transferexceptlast(){
while (queue1.size() > 1) {
queue2.push(queue1.front());
queue1.pop();
}
}
public:
MyStack() {

}


void push(int x) {
queue1.push(x);
}

// Removes the element on top of the stack and returns that element.
int pop() {
transferexceptlast();
int val = queue1.front();
queue1.pop();;
swap(queue1, queue2);
return val;
}

// Get the top element.
int top() {
transferexceptlast();
// The last element in queue1 is the top element of the stack
int topElement = queue1.front();
// Move the top element to queue2
queue2.push(topElement);
queue1.pop();
// Swap queue1 and queue2
swap(queue1, queue2);
return topElement;
}

// Returns whether the stack is empty.
bool empty() {
return queue1.empty();
}



};

2nd method :

class MyStack {
queue<int>q;
public:
//method is that : 👇👇
//When a new element is pushed, insert it at the end of the queue.
// Then, rotate all the elements (except the newly added elem) to the back of the queue, effectively bringing the newly added element to the front.
MyStack() {

}

void push(int x) {
q.push(x);
// rotate the elem to make the last elem as front elem
for(int i = 0; i<q.size()-1;++i){
q.push(q.front());
q.pop();
}

}

int pop() {
int topVal = q.front();
q.pop();
return topVal;

}

int top() {
return q.front();
}

bool empty() {
return q.empty();
}
};

code-nk
Автор

This code showing wrong answer
class MyStack {
public:
MyStack() {

}
queue<int> q1, q2;

void push(int x)
{
while(!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
q1.push(x);
while(!q2.empty())
{
q1.push(q2.front());
q2.pop();
}
}

int pop()
{
int val = q1.front();
q2.pop();
return val;
}

int top()
{
return q1.front();

}

bool empty() {
return q1.empty();
}
};

/**
* 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();
* bool param_4 = obj->empty();
*/

asurstark
Автор

Bhaiuya isme apne shurum me bola tha ki elements stack ke ulte daalne pdenge but fir wo kiye bina kaise ye program solve hua??

whatthepeeps
Автор

Sir please send c language code not c++
Please sir

darshanusdad