Skip to content

Commit c9e871c

Browse files
added queue implemenation using stack
1 parent 9a93c3a commit c9e871c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Queues/Implementation/usingStack.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
6+
7+
// queue implementation using two stacks
8+
stack<int> st1;
9+
stack<int> st2;
10+
11+
int i=0;
12+
while(i<5){
13+
//algo
14+
// 1. Push everything from stack1 to stack2
15+
// 2. insert x or num to stack 1
16+
// 3. Again push every number from stack2 to stack1
17+
int x;
18+
cin>>x;
19+
while(!st1.empty()){
20+
st2.push(st1.top());
21+
st1.pop();
22+
}
23+
st1.push(x);
24+
25+
while(!st2.empty()){
26+
st1.push(st2.top());
27+
st2.pop();
28+
}
29+
i++;
30+
}
31+
32+
while(!st1.empty()){
33+
cout<<st1.top()<<" ";
34+
st1.pop();
35+
}
36+
37+
38+
39+
}

0 commit comments

Comments
 (0)