-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackusingqueue.cpp
More file actions
44 lines (40 loc) · 826 Bytes
/
stackusingqueue.cpp
File metadata and controls
44 lines (40 loc) · 826 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
// implementing stack data structure using queue data structure
#include <iostream>
#include <queue>
using namespace std;
class stack
{
queue<int> primary_q, secondary_q;
public:
void push(int num)
{
secondary_q.push(num);
while (!primary_q.empty())
{
secondary_q.push(primary_q.front());
primary_q.pop();
}
queue<int> temp = primary_q;
primary_q = secondary_q;
secondary_q = temp;
}
int pop()
{
if (primary_q.empty())
return -1;
else
{
int num = primary_q.front();
primary_q.pop();
return num;
}
}
};
int main()
{
stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " " << s.pop() << " " << s.pop();
}