-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue_usingStack.cpp
82 lines (79 loc) · 1.53 KB
/
Queue_usingStack.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
You have to implement a template class queue using stack objects. The private members
of the Queue class are just stack objects and you can only use operations of stacks.
Implement the following functions:
1. IsFull:
2. IsEmpty:
3. Enqueue: Add an element in the queue.
4. Dequeue: Removes a front element from the queue.
5. Print: It will print all elements of the queue in FIFO order
NOTE: you are not allowed to use any array or link list in the queue class.
*/
#include<iostream>
#include<stack>
using namespace std;
template<class T>
class Queue {
stack<T> s1;
stack<T> s2;
public:
bool isEmpty() {
if (s1.empty())
return true;
return false;
}
bool enQueue(T val) {
s1.push(val);
return true;
}
bool deQueue(T& val) {
if (s1.empty())
return false;
else {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
val = s2.top();
s2.pop();
while (!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
return true;
}
}
void display() {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
while (!s2.empty()) {
cout << s2.top() << ",";
s2.pop();
}
}
};
int main() {
int value;
Queue<int> Q;
Q.enQueue(9);
Q.enQueue(8);
Q.enQueue(7);
Q.deQueue(value);
Q.enQueue(2);
Q.deQueue(value);
Q.enQueue(3);
Q.deQueue(value);
Q.enQueue(4);
Q.deQueue(value);
Q.enQueue(5);
Q.deQueue(value);
Q.deQueue(value);
Q.enQueue(50);
Q.enQueue(2);
Q.enQueue(10);
Q.display();
system("pause>nul");
return 0;
}