-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueArray.cpp
66 lines (59 loc) · 1.09 KB
/
queueArray.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
#include "queueArray.h"
void QueueArray::resize(int new_capacity) {
int* tempArray = new int[new_capacity];
for (int i = 0; i < size; ++i) {
tempArray[i] = arr[i];
}
delete[] arr;
arr = tempArray;
capacity = new_capacity;
}
QueueArray::QueueArray()
{
capacity = 16;
arr = new int[capacity];
size = 0;
}
QueueArray::~QueueArray() {
delete[] arr;
}
void QueueArray::enqueueEnd(int value) {
if (size == capacity) {
resize(capacity * 2);
}
arr[size++] = value;
}
int QueueArray::dequeueBeg() {
if (size < 1) {
cout << "Queue is empty" << endl;
return -1;
}
int value = arr[0];
for (int i = 1; i < size; ++i) {
arr[i - 1] = arr[i];
}
if (size == capacity / 4)
resize(capacity / 2);
return value;
}
void QueueArray::enqueueBeg(int value) {
size++;
if (size == capacity) {
resize(capacity * 2);
}
for (int i = 1; i < size; ++i) {
arr[i] = arr[i-1];
}
arr[0] = value;
}
int QueueArray::dequeueEnd() {
if (size < 1) {
cout << "Queue is empty" << endl;
return -1;
}
int value = arr[size-1];
size--;
if (size == capacity / 4)
resize(capacity / 2);
return value;
}