-
Notifications
You must be signed in to change notification settings - Fork 5
/
queue.cpp
61 lines (51 loc) · 1.1 KB
/
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
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10
template <class Type>
class Queue {
private:
Type *arr;
int head;
int tail;
int capacity;
public:
Queue(int size) {
arr = new Type[size];
head = 0;
tail = 0;
capacity = size;
}
void enqueue(Type item) {
if (isFull()) {
printf("Error, stack is full\n");
exit(EXIT_FAILURE);
}
arr[tail] = item;
tail = (tail+1) % capacity;
}
Type dequeue() {
if (isEmpty()) {
printf("Error, stack is empty\n");
exit(EXIT_FAILURE);
}
Type item = arr[head];
head = (head + 1) % capacity;
return item;
}
bool isFull() {
return ((tail + 1) % capacity == head);
}
bool isEmpty() {
return head == tail;
}
};
int main() {
Queue <string> q(5);
q.enqueue("brac");
q.enqueue("aiub");
q.enqueue("du");
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
}