Queue using two Stacks | GFG SDE Sheet | Anvita Bansal | GeeksforGeeks Practice

preview_player
Показать описание
Join Anvita in this insightful session as she guides you through the effective approach to handle Queue using two Stacks with in your data structures. Whether you're an experienced programmer or a novice, this video promises valuable insights into tackling this prevalent issue through the strategic use of Stack and Queue. Anvita will skillfully provide step-by-step explanations, accompanied by practical code examples, empowering you to grasp and seamlessly implement this technique in your coding projects.

Daily Videos are uploaded exclusively on GfG Practice. So subscribe today and start practicing!

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

Follow us and stay updated on everything happening in the world of geeks:

#GFGPractice #GeeksforGeeks #PracticeProblems #CodingQuestions #QueueUsingTwoStacks
Рекомендации по теме
Комментарии
Автор

I think you need to put null check on s2, before doing s2.pop() on line 76, maybe that's causing runtime error

man-lewc
Автор

we need to write a condition when stack1 is empty, so if s1 is empty then we need to return -1

code for the reference:
class StackQueue
{
Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>();

//Function to push an element in queue by using 2 stacks.
void Push(int x)
{
// Your code here
s1.push(x);
}


//Function to pop an element from queue by using 2 stacks.
int Pop()
{
// Your code here

if(!s2.isEmpty()){
return s2.pop();
}
else if(s1.isEmpty())return -1;

else{
while(!s1.isEmpty()){
s2.push(s1.pop());
}
return s2.pop();
}

}
}

waghakshay