-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue.cpp
99 lines (81 loc) · 1.46 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
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
#include "Queue.h"
#include <assert.h>
template <class T>
Queue<T>::Queue(void)
{
elements = 0;
capacity = 10;
arr = new T[capacity];
Front = Back = -1;
}
template <class T>
Queue<T>::Queue(int capacity)
{
elements = 0;
this->capacity = capacity;
arr = new T[capacity];
front = back = -1;
}
template <class T>
bool Queue<T>::empty()
{
return (elements == 0);
}
template <class T>
int Queue<T>::size()
{
return elements;
}
template <class T>
bool Queue<T>::full()
{
return (elements == capacity);
}
template <class T>
T Queue<T>::front()
{
assert(!empty());
return arr[Front];
}
template <class T>
T Queue<T>::back()
{
assert(!empty());
return arr[Back];
}
template <class T>
void Queue<T>::push(T newValue)
{
if (elements == capacity)
expand();
if (elements == 0)
Front = 0;
Back = (Back + 1) % capacity;
arr[Back] = newValue;
elements++;
}
template <class T>
void Queue<T>::expand()
{
T *newArr = new T[capacity * 2];
for (int i = 0; i < capacity; i++)
newArr[i] = arr[i];
capacity *= 2;
delete[] arr;
arr = newArr;
}
template <class T>
void Queue<T>::pop()
{
assert(!empty());
if (elements == 1)
Front = Back = -1;
else
Front = (Front + 1) % capacity;
elements--;
}
template <class T>
Queue<T>::~Queue()
{
delete[] arr;
}