AlgoPlus v0.1.0
Loading...
Searching...
No Matches
dequeue_list.h
1#ifndef QUEUE_H
2#define QUEUE_H
3
4#ifdef __cplusplus
5#include <iostream>
6#include <memory>
7#include <vector>
8#endif
9
13template <typename T> class dequeue_list {
14 private:
21 struct node {
22 T val;
23 std::shared_ptr<node> next;
24 std::shared_ptr<node> prev;
25 node(T key) : val(key), next(nullptr), prev(nullptr) {}
26 };
27
28 std::shared_ptr<node> root;
29 std::shared_ptr<node> tail;
30 size_t _size{0};
31
32 public:
38 inline explicit dequeue_list(std::vector<T> v = {}) noexcept : root(nullptr), tail(nullptr) {
39 if (!v.empty()) {
40 for (T& x : v) {
41 this->push_back(x);
42 }
43 }
44 }
45
51 inline explicit dequeue_list(const dequeue_list& q)
52 : root(q.root), tail(q.tail), _size(q._size) {}
53
60 root = q.root;
61 tail = q.tail;
62 _size = q._size;
63 return *this;
64 }
65
69 inline void clear() {
70 root = nullptr;
71 tail = nullptr;
72 _size = 0;
73 }
74
80 inline size_t size() { return _size; }
81
87 inline void push_back(T key) {
88 std::shared_ptr<node> nn = std::make_shared<node>(key);
89 if (!root) {
90 root = nn;
91 tail = nn;
92 _size++;
93 return;
94 } else {
95 tail->next = nn;
96 nn->prev = tail;
97 tail = nn;
98 _size++;
99 }
100 }
101
107 inline void push_front(T key) {
108 std::shared_ptr<node> nn = std::make_shared<node>(key);
109 if (!root) {
110 root = nn;
111 tail = nn;
112 _size++;
113 return;
114 } else {
115 root->prev = nn;
116 nn->next = root;
117 root = nn;
118 _size++;
119 }
120 }
121
127 inline T front() { return root->val; }
128
134 inline T back() { return tail->val; }
135
140 inline void pop_front() {
141 root = root->next;
142 root->prev = nullptr;
143 _size--;
144 }
145
150 inline void pop_back() {
151 tail = tail->prev;
152 tail->next = nullptr;
153 _size--;
154 }
155
156 class Iterator;
157
163 inline Iterator begin() { return Iterator(root); }
164
170 inline Iterator end() { return Iterator(nullptr); }
171};
172
176template <typename T> class dequeue_list<T>::Iterator {
177 private:
178 std::shared_ptr<node> curr_root;
179
180 public:
186 explicit Iterator(const std::shared_ptr<node>& q) noexcept : curr_root(q) {}
187
194 Iterator& operator=(std::shared_ptr<node> current) {
195 this->curr_root = current;
196 return *(this);
197 }
198
205 if (curr_root) {
206 curr_root = curr_root->next;
207 }
208 return *(this);
209 }
210
217 Iterator it = *this;
218 ++*(this);
219 return it;
220 }
221
229 bool operator!=(const Iterator& it) { return curr_root != it.curr_root; }
230
236 T operator*() { return curr_root->val; }
237};
238
239#endif
Iterator class.
Definition dequeue_list.h:176
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition dequeue_list.h:229
Iterator & operator++()
operator ++ for type Iterator
Definition dequeue_list.h:204
T operator*()
operator * for type Iterator
Definition dequeue_list.h:236
Iterator(const std::shared_ptr< node > &q) noexcept
Construct a new Iterator object.
Definition dequeue_list.h:186
Iterator operator++(int)
operator ++ for type Iterator
Definition dequeue_list.h:216
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition dequeue_list.h:194
void pop_back()
pop_back function removes the back from the queue
Definition dequeue_list.h:150
dequeue_list(const dequeue_list &q)
Copy constructor for dequeue list class.
Definition dequeue_list.h:51
size_t size()
size functon
Definition dequeue_list.h:80
void pop_front()
pop_front function removes the front from the dequeue
Definition dequeue_list.h:140
dequeue_list & operator=(const dequeue_list &q)
operator = for dequeue list class
Definition dequeue_list.h:59
dequeue_list(std::vector< T > v={}) noexcept
Construct a new dequeue list object.
Definition dequeue_list.h:38
T back()
back function
Definition dequeue_list.h:134
void push_back(T key)
push_back function
Definition dequeue_list.h:87
T front()
top function
Definition dequeue_list.h:127
void push_front(T key)
push_front function
Definition dequeue_list.h:107
Iterator begin()
pointer to the front of the dequeue
Definition dequeue_list.h:163
Iterator end()
pointer to the end of the dequeue
Definition dequeue_list.h:170
void clear()
clear function
Definition dequeue_list.h:69