Implement Queue using Stacks | 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 Queue using Stacks.

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

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

*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
leetcode problems js
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
Рекомендации по теме
Комментарии
Автор

sir 318 virtual contest ke questions pe video baniyye . it will be really helpful

showsshorts
Автор

ye pop vale operation me apne peek () function ko call kiya hai kya niche vala

thenextlevel
Автор

class MyQueue {
stack<int>input, output;
// when doing peek or pop we have to fill the elements in a different container (so we will use two stacks for this) as in stack the element that should be on front in queue are at the last
void transfer() {
if (output.empty()) {
while (!input.empty()) {
output.push(input.top());
input.pop();
}
}
}
public:

MyQueue() {}

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

int pop() {
transfer();
int front = output.top();
output.pop();
return front;

}

int peek() {
transfer();
return output.top();
}

bool empty() {
return input.empty()&& output.empty();
}
};

code-nk
Автор

Beautifully explained Prince bhai, endsem chal raha tha toh consistency thoda break ho gaya mera
My code :
class MyQueue {
public:

stack<int> input, output;

MyQueue() {

}

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

int pop() {
int ans = peek();
output.pop();
return ans;
}

int peek() {
if (output.empty() == true){
while (!input.empty()){
output.push(input.top());
input.pop();
}
}
return output.top();
}

bool empty() {
if (input.empty() == true and output.empty() == true){
return true;
}
return false;
}
};

pritishpattnaik
Автор

there is catch
input.push(10);
input.push(20);
input.push(30);
input.push(40);

if i you call pop() method then it should return 10 but in your code it will return 40; if you never call peek() method before calling pop() method

iftekhar_ansari
Автор

1st method implemented successfully bro

devsharma
Автор

Bhai ye jo last me solution btaya iski complexity bhi toh O(N) aaegi?

raufurrahmankhan
Автор

Prince bhai complexity iski bhi O(N) hogai kya?

raufurrahmankhan
Автор

Bhai Video to theek h, but leetcode ka logo kahi or se inspired lag raha h

sarangtamrakar
Автор

i think this solution but fell lazy to implement

himanshutiwari
Автор

mera bhi run kar gya ...answer sahi aa gya but koi test case fail ho gya tha.

shaantyagi
Автор

maine second solution soch liya implementation me problem ho rahi thi

oqant
Автор

Nhi socha maine...Meri galti I should have invested more time

manikchadha
Автор

Showing TLE, code not working class MyQueue {
public:

stack<int> input, output;
MyQueue() {

}

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

int pop()
{
int val =output.top();
output.pop();
return val;
}

int peek()
{
if(output.empty())
{
while(input.empty()==false)
output.push(input.top());
input.pop();
}
return output.top();
}

bool empty()
{
return input.empty() && output.empty();
}
};

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

asurstark