Queue using Stack | C++ Placement Course | Lecture 24.3

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

After you pop the elements are in s2 but now if you do push the new element will be added to s1 but as the first element of queue now if you do pop again the element we added in last will be popped so there's problem with push function and you did'nt add peek() funtion..
Here's the proper code with all things taken care of...Thank you

#include <iostream>
#include<stack>
using namespace std;

class queue
{
stack<int> s1;
stack<int> s2;

public:
void enqueue(int x)
{
if (!s2.empty())
{
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
}

s1.push(x);
}

void dequeue()
{
if(s1.empty() and s2.empty()){
cout<<"queue is empty";
return;
}

if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}

s2.pop();

return;
}

int peek()
{
if (s1.empty() and s2.empty())
{
cout << "queue is empty";
return -1;
}

if (s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}

int topval = s2.top();

return topval;
}

bool empty()
{
if (s1.empty() and s2.empty())
{
return true;
}
return false;
}
};

int main()
{
queue q;
int p, el;
cin >> p;
for (int i = 0; i < p; i++)
{
cin >> el;
q.enqueue(el);
}

q.dequeue();
cout << q.peek() << " ";
q.dequeue();
cout << q.peek() << " ";
cout << q.empty();
return 0;
}

naveensg
Автор

Please make a seperate playlist on C++ STL libraries.

skundverma
Автор

aap lge rho kuki aap ho toh hum hai
and yaa thnx jo aap hme itna kuch provide krte ho
aur kiya h abhi tk
thnx a lot for this

QniqueTales
Автор

Hey guys i am doing this playlist for dsa preparation in 2024. Now I am at 80th video hope i will soon complete this playlist. Great resource by Aman bhaiya and whole apna college team. Thank you.

sachinkashi
Автор

after we popped 1 and added how was it added in stack 2 as stack 2 was not empty and we need it empty acc. to the condition

Aryankumar-hxxb
Автор

For the first time I felt the explanation was very poor

nkbanerjee
Автор

s.pop() ku likha aap ne?
when you using recursion to pop the element

abhaysharma
Автор

So plz .tell me
Kon sa laptop beat rhega

absurajrathaur
Автор

Don't know but my code is not giving any output in vs code

omkarsase
Автор

Bhaiya please make course on grafic design

vashusen
Автор

What is use of implementing such a costly approach?

sumitvelaskar
Автор

bhaiya index me apne btaya tha phase 1 and 2 feb last tk dal doge ????

adarshsrivastav
Автор

Aman bhaiyya yaar apni kaksha pe course upload kar do yaar me apke bharose hi betha hu

Fearlessinevitable
Автор

Bhai group/team me coding kese krte hen

MohammadAhmad-wiey
Автор

why do we use 2 stacks and not just 1 stack

ashushimpi
Автор

When will java placement course start?

prathvishetty
Автор

3:52 i see first time int32_t main(). it is work

TodayContestSolution
Автор

Sir computer networking pa ve ak course laya please

kuchvee
Автор

In the first part shouldn't we need to move elements of stack 2 in stack 1 after poping out of element

dharmenderkumar
Автор

I got error in line no. 19
Plzz crrct me

ashishchauhan