-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabCycle2-5.cpp
107 lines (99 loc) · 2.35 KB
/
LabCycle2-5.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "LabCycle2-5.h"
template <typename T, int MAX_SIZE>
DequeueArray<T, MAX_SIZE>::DequeueArray() {
front = -1;
rear = -1;
itemCount = 0;
}
template <typename T, int MAX_SIZE>
void DequeueArray<T, MAX_SIZE>::insertFront(T element) {
if (isFull()) {
cout << "DEQUEUE Overflow!\n";
return;
}
if (front == -1) {
front = rear = 0;
} else if (front == 0) {
front = MAX_SIZE - 1;
} else {
front--;
}
arr[front] = element;
itemCount++;
display();
}
template <typename T, int MAX_SIZE>
void DequeueArray<T, MAX_SIZE>::insertRear(T element) {
if (isFull()) {
cout << "DEQUEUE Overflow!\n";
return;
}
if (rear == -1) {
front = rear = 0;
} else if (rear == MAX_SIZE - 1) {
rear = 0;
} else {
rear++;
}
arr[rear] = element;
itemCount++;
display();
}
template <typename T, int MAX_SIZE>
T DequeueArray<T, MAX_SIZE>::deleteFront() {
if (isEmpty()) {
cout << "DEQUEUE Underflow!\n";
return T();
}
T deletedItem = arr[front];
if (front == rear) {
front = rear = -1;
} else if (front == MAX_SIZE - 1) {
front = 0;
} else {
front++;
}
itemCount--;
display();
return deletedItem;
}
template <typename T, int MAX_SIZE>
T DequeueArray<T, MAX_SIZE>::deleteRear() {
if (isEmpty()) {
cout << "DEQUEUE Underflow!\n";
return T();
}
T deletedItem = arr[rear];
if (front == rear) {
front = rear = -1;
} else if (rear == 0) {
rear = MAX_SIZE - 1;
} else {
rear--;
}
itemCount--;
display();
return deletedItem;
}
template <typename T, int MAX_SIZE>
bool DequeueArray<T, MAX_SIZE>::isEmpty() {
return itemCount == 0;
}
template <typename T, int MAX_SIZE>
bool DequeueArray<T, MAX_SIZE>::isFull() {
return itemCount == MAX_SIZE;
}
template <typename T, int MAX_SIZE>
void DequeueArray<T, MAX_SIZE>::display() {
if (isEmpty()) {
cout << "DEQUEUE is empty.\n";
return;
}
cout << "DEQUEUE elements: ";
int i = front;
do {
cout << arr[i] << " ";
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
cout << endl;
}