-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharQueue.cpp
44 lines (38 loc) · 1.18 KB
/
CharQueue.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
#include "CharQueue.h"
CharQueue::CharQueue() {
front = 0;
back = 0;
}
void CharQueue::enqueue(char c) {
if (!isFull()) {
// arr[back] = c;
// back = (back + 1) % QUEUE_SIZE;
back = (back + 1) % (MAX_QUEUE + 1); // update to use MAX_QUEUE
arr[back] = c;
} else {
cout << "Queue is full!" << endl;
}
}
char CharQueue::dequeue() {
if (!isEmpty()) {
char toReturn = arr[front];
// front = (front + 1) % QUEUE_SIZE;
front = (front + 1) % (MAX_QUEUE + 1); // update to use MAX_QUEUE
// return toReturn;
return arr[front];
} else {
cout << "Queue is empty!" << endl;
return '\0'; // return a default char as a fallback
}
}
bool CharQueue::isEmpty() {
// Queue is empty only if both stacks are empty.
// return stackIn.isEmpty() && stackOut.isEmpty();
return back == front;
}
bool CharQueue::isFull() {
// queue is full if the combined size of both stacks is >= MAX_QUEUE.
// return (stackIn.size() + stackOut.size()) >= MAX_QUEUE;
// return (back + 1) % QUEUE_SIZE == front;
return (back + 1) % (MAX_QUEUE + 1) == front; // update to use MAX_QUEUE
}