-
Notifications
You must be signed in to change notification settings - Fork 2
/
Queue.h
81 lines (68 loc) · 1.47 KB
/
Queue.h
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
// Author: Christopher Lee Jackson
#ifndef __QUEUE_H_INCLUDED__
#define __QUEUE_H_INCLUDED__
#include <limits>
#include <iostream>
#include <cmath>
using namespace std;
/*
*
* QUEUE CLASS
*
*/
class Queue{
public:
unsigned int size;
unsigned int head;
unsigned int tail;
unsigned int max;
int* array;
Queue(int s=10){array=new int[s]; max=s; size=0; head=0; tail=0;}
~Queue(){delete[] array;}
int front(){return array[head];}
int back(){return array[tail];}
void push(int x);
int pop();
unsigned int getSize(){return size;}
bool empty(){return (size==0);}
bool full(){return (size==max);}
void reset(){size=0; head=0; tail=0;}
bool exists(int value);
};
inline bool Queue::exists(int value) {
for(unsigned int i = 0; i < size; i++) {
if(array[i] == value)
return true;
}
return false;
}
inline void Queue::push(int x) {
int temp;
if(empty()) {
size++;
array[tail] = x;
}
else if (full()){
// size stays the same but the tail shifts along with the head.
array[(++tail)%max] = x;
temp = (++head)%max;
head = temp;
}
else {
size++;
array[(++tail)%max] = x;
}
}
inline int Queue::pop() {
int tmp;
if(empty()) {
cerr << "Queue is empty. Nothing to pop.";
exit(-1);
}
int temp = head;
tmp = (++head)%max;
head = tmp;
size--;
return array[temp];
}
#endif