-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
55 lines (51 loc) · 1.07 KB
/
queue.cpp
File metadata and controls
55 lines (51 loc) · 1.07 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
#ifndef QUEUE_CPP
#define QUEUE_CPP
//#include <cstddef>
template<typename T>
class Queue {
struct Node {
T value;
Node* next;
Node(T value, Node* next = nullptr) : value(value), next(next) {}
};
Node* _front;
Node* _back;
unsigned int _length;
public:
Queue() {
_length = 0;
_front = nullptr;
_back = nullptr;
}
T front() const {
assert(_length>0);
return _front -> value;
}
T back() const {
assert(_length>0);
return _back -> value;
}
void push(T value) {
Node* node = new Node(value);
if(this -> _front != nullptr)
this -> _front -> next = node;
else
this -> _back = node;
this -> _front = node;
_length++;
}
void pop() {
assert(_length>0);
Node* node = _back;
_back = _back -> next;
delete node;
_length--;
}
unsigned int size() const {
return _length;
}
bool empty() const {
return _length == 0;
}
};
#endif