-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
190 lines (155 loc) · 4.96 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef QUEUE_H
#define QUEUE_H
#include "LinkedListFunctions.h"
#include <cassert>
template <class T>
class Queue {
public:
class Iterator{
public:
friend class Queue; //give access to list to access _ptr
Iterator(Node<T>* p = nullptr) : _ptr(p) {}
operator bool() const { //casting operator: true if _ptr
//not nullptr
return _ptr != nullptr;
}
T& operator*() { //dereference operator
assert(_ptr);
return _ptr->_item;
}
T* operator->() { //member access operator
return &_ptr;
}
bool is_null() { //true if _ptr is NULL
return _ptr == nullptr;
}
friend bool operator !=(const Iterator& left,
const Iterator& right) {
//true if left != right
return left._ptr != right._ptr;
}
friend bool operator ==(const Iterator& left,
const Iterator& right) {
//true if left == right
return left._ptr == right._ptr;
}
Iterator& operator++() { //member operator: ++it;
//or ++it = new_value
assert(_ptr);
_ptr = _ptr->_next;
return *this;
}
Iterator& operator--() { //member operator: --it;
//or --it = new_value
assert(_ptr);
_ptr = _ptr->_prev;
return *this;
}
friend Iterator operator++(Iterator& it,
int unused) {
//friend operator: it++
assert(it._ptr);
Iterator hold;
hold = it;
it._ptr = it._ptr->_next;
return hold;
}
friend Iterator operator--(Iterator& it,
int unused) {
//friend operator: it--
assert(it._ptr);
Iterator hold;
hold = it;
it._ptr = it._ptr->_prev;
return hold;
}
private:
Node<T>* _ptr; //pointer being encapsulated
};
Queue() : _head(nullptr), _tail(nullptr), _size(0) {}
// BIG 3
Queue(const Queue<T>& other);
~Queue();
Queue<T>& operator=(const Queue& rhs);
void push(T item);
T pop();
bool empty();
T front();
unsigned int size();
Node<T>* _find(const T& item);
template <class E>
friend ostream& operator<<(ostream& outs, const Queue<E>& q);
private:
Node<T>* _head;
Node<T>* _tail;
unsigned int _size;
};
template <class T>
ostream& operator<<(ostream& outs, const Queue<T>& q) {
return print_list(q._head, outs);
}
//=============================== BIG 3 =======================================
template <class T>
Queue<T>::Queue(const Queue<T> &other) {
this->_head = nullptr;
this->_tail = nullptr;
this->_size = 0;
Node<T>* current = other._head;
while(current) {
this->push(current->_item);
current = current->_next;
}
}
template <class T>
Queue<T>::~Queue() {
while(_head)
this->pop();
}
template <class T>
Queue<T>& Queue<T>::operator=(const Queue<T>& rhs) {
this->~Queue();
Node<T>* current = rhs._head;
while(current) {
this->push(current->_item);
current = current->_next;
}
return *this;
}
//=============================================================================
template <class T>
void Queue<T>::push(T item) {
if(_tail) {
insert_after(this->_head, this->_tail, item);
_tail = _tail->_next;
}
else {
insert_head(this->_head, item);
_tail = this->_head;
}
_size++;
}
template <class T>
T Queue<T>::pop() {
_size--;
if(!_head->_next)
_tail = nullptr;
return delete_head(this->_head);
}
template <class T>
bool Queue<T>::empty() {
return this->_head == nullptr;
}
template <class T>
T Queue<T>::front() {
assert(_head);
return this->_head->_item;
}
template <typename T>
unsigned int Queue<T>::size() {
return this->_size;
}
template <class T>
Node<T>* Queue<T>::_find(const T& item) {
return find(this->_head, item);
}
#endif // QUEUE_H