-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingQueue.cpp
More file actions
51 lines (48 loc) · 956 Bytes
/
StackUsingQueue.cpp
File metadata and controls
51 lines (48 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
// Stack using queue
class Stack1{
queue<int> q1;
queue<int> q2;
int N;
public:
Stack1(){
N = 0;
}
void push(int x){
q2.push(x);
N++;
while(!q1.empty()){
q2.push(q1.front());
q1.pop();
}
queue<int> temp = q2;
q2 = q1;
q1 = temp;
}
int pop(){
int item = q1.front();
q1.pop();
N--;
return item;
}
int size(){
return N;
}
int peek(){
return q1.front();
}
};
int main(){
Stack1 st1;
st1.push(10);
st1.push(20);
st1.push(30);
st1.push(40);
st1.pop();
cout << st1.peek() << endl;
st1.pop();
cout << st1.peek() << endl;
cout << "Size " << st1.size() << endl;
return 0;
}