-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyQueue.cpp
53 lines (45 loc) · 1.46 KB
/
MyQueue.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
/*
CS3103 Assginment 2
Completed by:
Quintus Kilbourn (54871935)
Rohit Shyla Kumar (54581876)
File Name - MyQueue.cpp
Description - Implementation of standard FIFO queue functions
*/
#include "MyQueue.h"
//Constructor performs initialization when a new queue is instantiated
MyQueue::MyQueue() {
front = 0;
size = 0;
}
//We use inline functions to optimize small processes
inline bool MyQueue::isEmpty() {
return (size == 0); //if the queue has size 0, it is considered to be empty
}
inline bool MyQueue::isFull() {
return (size == QLEN); //if the queue has the maximum permitted elements, it is considered full
}
bool MyQueue::EnQueue(int token) {
//Cannot add a token to a full queue
if (!isFull()) {
tokens[(front + size) % QLEN] = token; // %QLEN allows us to use empty spaces at the front of the queue making this a circular queue
size++;
return 1; //Operation was successful
}
else {
return 0; //Operation failed
}
}
int MyQueue::DeQueue() {
int retVal;
//Cannot remove a token from an empty queue
if (!isEmpty()) {
retVal = tokens[front];
front = (front + 1) % QLEN; //So the front wraps around the queue and may never go out of bounds of the array
size--;
return retVal; //Return the sequence number of the token at the fron of the queue
}
else {
return -1; //Operation failed. We return -1 instead of 0 as a token may have sequence number 0 and other functions could misinterpret this result
}
}