-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueusingstack.cpp
More file actions
49 lines (43 loc) · 908 Bytes
/
queueusingstack.cpp
File metadata and controls
49 lines (43 loc) · 908 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
//implementing queue data structure using stack
#include <iostream>
#include <stack>
using namespace std;
class queue
{
stack<int> primary_s, secondary_s;
public:
void enqueue(int num)
{
while (!primary_s.empty())
{
secondary_s.push(primary_s.top());
primary_s.pop();
}
primary_s.push(num);
while (!secondary_s.empty())
{
primary_s.push(secondary_s.top());
secondary_s.pop();
}
}
int dequeue()
{
if (primary_s.empty())
return -1;
int num = primary_s.top();
primary_s.pop();
return num;
}
};
int main()
{
queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
}