class MyQueue { private Stack input; private Stack output; public MyQueue() { this.input = new Stack(); this.output = new Stack(); } public void push(int x) { input.push(x); } public int pop() { if (!output.isEmpty()) return output.pop(); while(!input.isEmpty()){ output.push(input.pop()); } return output.pop(); } public int peek() { if (output.isEmpty()) while (!input.isEmpty()) output.push(input.pop()); return output.peek(); } public boolean empty() { return (input.isEmpty() && output.isEmpty()); } } // thank me later!
Whenever I see your video related to DSA, I am satisfied with the concept.
All your explanations are just best
please make a playlist on Stack, queue, HashTables tries etc. as your teaching is awesome please continue your ds playlist
there are problems available on stacks and queues, just not in a playlist...will do that eventually :)
@@nikoo28 sir please make a playlist so helpful
nice explanation with visuals and clear understanding, love this.
Great explanation sir ❤👍
You have playlists on all other data structures
Can you make a playlist on stacks & queues too?
i will try to find more problems that are explicitly on stacks and queues.
He is the best
nice explanation..... please make playlist for linked-list.. lot's of people are struggling in linked-list..
Thanks!
did you already check this out: czcams.com/play/PLFdAYMIVJQHN6J5-OCh7pbG0o8WHC9so3.html
wonderfull broo
class MyQueue {
private Stack input;
private Stack output;
public MyQueue() {
this.input = new Stack();
this.output = new Stack();
}
public void push(int x) {
input.push(x);
}
public int pop() {
if (!output.isEmpty())
return output.pop();
while(!input.isEmpty()){
output.push(input.pop());
}
return output.pop();
}
public int peek() {
if (output.isEmpty())
while (!input.isEmpty())
output.push(input.pop());
return output.peek();
}
public boolean empty() {
return (input.isEmpty() && output.isEmpty());
}
}
// thank me later!
very interesting.