1#ifndef DOUBLY_LINKED_LIST_H
2#define DOUBLY_LINKED_LIST_H
4#ifdef ENABLE_LINKED_LIST_VISUALIZATION
5#include "../../visualization/list_visual/linked_list_visualization.h"
27 : root(nullptr), tail(nullptr) {
28 if (!_elements.empty()) {
29 for (T& x : _elements) {
40 : root(l.root), tail(l.tail), _size(l._size) {}
58 inline bool empty() {
return root ==
nullptr; }
64 inline size_t size() {
return _size; }
130 std::shared_ptr<node> head = l.root;
132 out << head->val <<
' ';
148 std::shared_ptr<node> next;
149 std::shared_ptr<node> prev;
150 node(T key = 0) : val(key), next(nullptr), prev(nullptr) {}
152 std::shared_ptr<node> root;
153 std::shared_ptr<node> tail;
156 std::string generate();
163 std::shared_ptr<node> t = root;
164 while (t !=
nullptr && t->val != key) {
167 return (t ==
nullptr || t->val != key) ? false :
true;
173 std::shared_ptr<node> p = std::make_shared<node>(key);
176 if (tail !=
nullptr) {
179 if (root ==
nullptr) {
187 std::shared_ptr<node> p = std::make_shared<node>(key);
190 if (root !=
nullptr) {
193 if (tail ==
nullptr) {
201 if (root ==
nullptr) {
204 std::shared_ptr<node> head = root;
205 while (head && head->val != key) {
208 if (head ==
nullptr) {
211 if (head->next !=
nullptr) {
215 head->next->prev = head->prev;
217 if (head->prev !=
nullptr) {
221 head->prev->next = head->next;
226 std::vector<T> _elements;
230 std::shared_ptr<node> head = root;
232 _elements.push_back(head->val);
239 std::shared_ptr<node> current = root;
240 std::shared_ptr<node> temp{
nullptr};
242 while (current !=
nullptr) {
243 temp = current->prev;
244 current->prev = current->next;
245 current->next = temp;
246 current = current->prev;
254template <
typename T> std::string doubly_linked_list<T>::generate() {
256 gen +=
"rankdir=LR;";
258 gen +=
"node [shape=record;]";
260 std::vector<T> els = this->elements();
261 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
262 for (
auto& x : els) {
264 gen +=
" [label=<{ ";
270 std::shared_ptr<node> curr = root;
274 gen += curr->next->val;
275 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
278 gen += curr->next->val;
281 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
287 for (
auto& x : els) {
288 gen += std::to_string(x);
289 gen +=
" [label=<{ ";
290 gen += std::to_string(x);
295 std::shared_ptr<node> curr = root;
297 gen += std::to_string(curr->val);
299 gen += std::to_string(curr->next->val);
300 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
303 gen += std::to_string(curr->next->val);
305 gen += std::to_string(curr->val);
306 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
315#ifdef LINKED_LIST_VISUALIZATION_H
317 std::string generated = this->generate();
318 linked_list_visualization::visualize(generated);
327 std::shared_ptr<node> curr_root;
335 explicit Iterator(
const std::shared_ptr<node>& l) noexcept : curr_root(l) {}
344 this->curr_root = current;
355 curr_root = curr_root->next;
378 curr_root = curr_root->prev;
Iterator class.
Definition doubly_linked_list.h:325
Iterator operator--(int)
operator – for type iterator
Definition doubly_linked_list.h:388
Iterator operator++(int)
operator ++ for type Iterator
Definition doubly_linked_list.h:365
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition doubly_linked_list.h:343
Iterator(const std::shared_ptr< node > &l) noexcept
Construct a new Iterator object.
Definition doubly_linked_list.h:335
Iterator & operator--()
operator – for type Iterator
Definition doubly_linked_list.h:376
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition doubly_linked_list.h:401
T operator*()
operator * for type Iterator
Definition doubly_linked_list.h:408
Iterator & operator++()
operator ++ for type Iterator
Definition doubly_linked_list.h:353
Iterator end()
pointer that points to end
Definition doubly_linked_list.h:80
void push_back(T key)
push_back function.
Definition doubly_linked_list.h:172
doubly_linked_list & operator=(const doubly_linked_list &l)
operator = for doubly linked list class
Definition doubly_linked_list.h:47
doubly_linked_list(std::vector< T > _elements={}) noexcept
doubly_linked_list class constructor
Definition doubly_linked_list.h:26
doubly_linked_list(const doubly_linked_list &l)
copy constructor for the doubly_linked_list class
Definition doubly_linked_list.h:39
bool empty()
empty function.
Definition doubly_linked_list.h:58
size_t size()
size function. Returns the size of the list.
Definition doubly_linked_list.h:64
void push_front(T key)
push_front function.
Definition doubly_linked_list.h:186
void visualize()
visualize function returns a .dot file that can be previewd with graphviz plugin in vscode
void reverse()
reverse function. reverses the linked list.
Definition doubly_linked_list.h:238
Iterator begin()
pointer that points to begin
Definition doubly_linked_list.h:73
friend std::ostream & operator<<(std::ostream &out, doubly_linked_list< T > &l)
<< operator for the doubly_linked_list class.
Definition doubly_linked_list.h:128
bool search(T key)
search function.
Definition doubly_linked_list.h:159
std::vector< T > elements()
elements function.
Definition doubly_linked_list.h:225
void erase(T key)
erase function.
Definition doubly_linked_list.h:200