forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
91 lines (80 loc) · 2.4 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
#include<iostream>
#include<memory>
#include<cassert>
namespace my {
/**
* implementation using linked list
* [value][next] -> [value][next] -> ... -> [value][next]
* (front Node) (intermediat Nodes) (rear Node)
*/
template<typename T>
struct Node {
/**
* next: will store right Node address
*/
T value;
std::shared_ptr<Node<T>> next;
Node(const T& V) : value(V) { }
};
template<typename T>
class queue {
private:
/**
* front_pointer: points to left most node
* count: keeps track of current number of elements present in queue
* rear_pointer: points to most recent Node added into the queue, which is right most Node
*/
std::shared_ptr<Node<T>> front_pointer;
std::shared_ptr<Node<T>> rear_pointer;
size_t count;
public:
queue() : count(0ULL) { }
void enqueue(const T& element) {
auto new_node = std::make_shared<Node<T>>(element);
if (count > 0) {
rear_pointer->next = new_node;
rear_pointer = new_node;
} else {
rear_pointer = front_pointer = new_node;
}
count = count + 1;
}
void dequeue() {
if (count > 1) {
front_pointer = front_pointer->next;
count = count - 1;
} else if (count == 1) {
front_pointer.reset();
rear_pointer.reset();
count = count - 1;
}
}
T& front() {
assert(count > 0 && "calling front on an empty queue");
return front_pointer->value;
}
T const& front() const {
assert(count > 0 && "calling front on an empty queue");
return front_pointer->value;
}
size_t size() const { return count; }
bool empty() const { return count == 0; }
~queue() {
while (front_pointer.get() != nullptr) {
front_pointer = front_pointer->next;
}
}
};
}
int main() {
my::queue<int> intQueue;
intQueue.enqueue(4);
intQueue.enqueue(5);
intQueue.enqueue(9);
int frontElement = intQueue.front();
intQueue.dequeue();
std::cout << frontElement << '\n';
std::cout << intQueue.size() << '\n';
std::cout << intQueue.front() << '\n';
return 0;
}