-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstack_queue.cpp
69 lines (56 loc) · 1.37 KB
/
stack_queue.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
#include <iostream>
#include "stack_dll.h"
#include "queue_array.h"
using namespace std;
void print(QueueArray<int>& queue);
void reverse(QueueArray<int>& queue);
int main() {
QueueArray<int> queue;
for (int i = 0; i < 20; i++)
queue.enqueue(i);
cout << "Queue: ";
print(queue);
cout << endl;
cout << "Reversed: ";
reverse(queue);
print(queue);
cout << endl;
return 0;
}
void print(QueueArray<int>& queue) {
for (int i = 0; i < queue.get_size(); i++) {
cout << queue.get_first() << " ";
queue.enqueue(queue.dequeue());
}
}
// OTHER POSSIBLE IMPLEMENTATIONS:
//
// 1)
// void print(QueueArray<int>& queue) {
// QueueArray<int> temp(queue);
//
// while (!temp.is_empty())
// cout << temp.dequeue() << " ";
// }
//
// 2)
// void print(QueueArray<int>& queue) {
// QueueArray<int> temp;
//
// while (!queue.is_empty()) {
// cout << queue.get_first() << " ";
// temp.enqueue(queue.dequeue());
// }
//
// queue = temp;
// }
void reverse(QueueArray<int>& queue) {
StackDLL<int> stack;
while (!queue.is_empty())
stack.push(queue.dequeue());
while (!stack.is_empty())
queue.enqueue(stack.pop());
}
// Note. To reverse a Stack, we can use a Queue
// following the same logic used for reversing
// the queue in the above function.