-
Notifications
You must be signed in to change notification settings - Fork 2
/
Queue.cpp
70 lines (62 loc) · 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
62
63
64
65
66
67
68
69
70
/*
* Queue.cpp
*
* Created on: Nov 19, 2016
* Author: rocco
*/
#include "Queue.h"
Queue::Queue() : first(nullptr), last(nullptr), MAX_SIZE(10000), currSize(0) {}
Queue::Queue(int sizeIn) : first(nullptr), last(nullptr), MAX_SIZE(sizeIn), currSize(0) {}
void Queue::enqueue(Node* lastIn)
{
if (isEmpty())
{
first = lastIn;
last = lastIn;
currSize++;
}
else
{
if (currSize == MAX_SIZE)
{
last->setNext(lastIn);
last = lastIn;
dequeue();
}
else
{
last->setNext(lastIn);
last = lastIn;
currSize++;
}
}
}
void Queue::prepare()
{
for(int j=0; j<MAX_SIZE;j++)
{
enqueue(new Node(100, nullptr));
}
}
Node* Queue::dequeue()
{
if (isEmpty()) { return nullptr; }
else
{
Node* data = first;
//delete first;
first = first->getNext();
return data;
}
}
int Queue::getSize() { return MAX_SIZE; }
void Queue::reset()
{
while(!isEmpty())
{
Node * temp = first->getNext();
delete first;
first = temp;
}
}
bool Queue::isEmpty() { return first == nullptr; }