-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueArray.h
More file actions
81 lines (65 loc) · 1.47 KB
/
Copy pathQueueArray.h
File metadata and controls
81 lines (65 loc) · 1.47 KB
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
// Made by @DaniDiazTech
#include<iostream>
// Queue Array implementation
// First in first out : FIFO principle
// Methods
// enqueue: O(1) (amortized)
// dequeue: O(1) -> returns
// front: O(1)
// size: O(1)
// empty: O(1)
template<typename T> struct Queue{
// front in place, back off by one
int _front, _back, _size, capacity;
T *data;
Queue(){
_front = _back = _size = 0;
capacity = 16;
data = new T[capacity];
}
int size(){
return _size;
}
bool empty(){
return size() == 0;
}
// returns front element
T front(){
if (empty())
throw std::out_of_range("Can't access empty queue");
return data[_front];
}
// Removes and returns front element
T dequeue(){
if (empty())
throw std::out_of_range("Can't dequeue empty queue");
_size--;
T el = data[_front];
_front = (_front + 1) % capacity;
return el;
}
// Inserts element in back
void enqueue(T element){
if (size()== capacity){
// Resize array
T* new_data = new T[capacity * 2];
for (int i = 0; i < size(); i++){
new_data[i] = data[(i + _front) % capacity];
}
_front = 0;
_back = capacity;
capacity *= 2;
std::swap(new_data, data);
delete [] new_data;
}
_size++;
data[_back] = element;
_back = (_back + 1) % capacity;
}
void print_array(){
for (int i = 0 ; i < capacity; i++){
std::cout << data[i] << " ";
}
std::cout << std::endl;
}
};