1#ifndef DOUBLY_LINKED_LIST_H
2#define DOUBLY_LINKED_LIST_H
4#ifdef LINKED_LIST_VISUALIZATION_H
5#include "../../visualization/list_visual/linked_list_visualization.h"
27 : root(nullptr), tail(nullptr) {
28 if (!_elements.empty()) {
29 for (T &x : _elements) {
61 bool empty() {
return root ==
nullptr; }
67 size_t size() {
return _size; }
96 void push_back(T key);
102 void push_front(T key);
114 std::vector<T> elements();
133 std::shared_ptr<node> head = l.root;
135 out << head->val <<
' ';
151 std::shared_ptr<node> next;
152 std::shared_ptr<node> prev;
153 node(T key = 0) : val(key), next(nullptr), prev(nullptr) {}
155 std::shared_ptr<node> root;
156 std::shared_ptr<node> tail;
159 std::string generate();
166 std::shared_ptr<node> t = root;
167 while (t !=
nullptr && t->val != key) {
170 return (t ==
nullptr || t->val != key) ? false :
true;
176 std::shared_ptr<node> p = std::make_shared<node>(key);
179 if (tail !=
nullptr) {
182 if (root ==
nullptr) {
190 std::shared_ptr<node> p = std::make_shared<node>(key);
193 if (root !=
nullptr) {
196 if(tail ==
nullptr) {
204 if (root ==
nullptr) {
207 std::shared_ptr<node> head = root;
208 while (head && head->val != key) {
211 if (head ==
nullptr) {
214 if (head->next !=
nullptr) {
218 head->next->prev = head->prev;
220 if (head->prev !=
nullptr) {
224 head->prev->next = head->next;
229 std::vector<T> _elements;
233 std::shared_ptr<node> head = root;
235 _elements.push_back(head->val);
242 std::shared_ptr<node> current = root;
243 std::shared_ptr<node> temp{
nullptr};
245 while (current !=
nullptr) {
246 temp = current->prev;
247 current->prev = current->next;
248 current->next = temp;
249 current = current->prev;
259 gen +=
"rankdir=LR;";
261 gen +=
"node [shape=record;]";
263 std::vector<T> els = this->elements();
264 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
265 for (
auto &x : els) {
267 gen +=
" [label=<{ ";
273 std::shared_ptr<node> curr = root;
277 gen += curr->next->val;
278 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
281 gen += curr->next->val;
284 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
290 for (
auto &x : els) {
291 gen += std::to_string(x);
292 gen +=
" [label=<{ ";
293 gen += std::to_string(x);
298 std::shared_ptr<node> curr = root;
300 gen += std::to_string(curr->val);
302 gen += std::to_string(curr->next->val);
303 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
306 gen += std::to_string(curr->next->val);
308 gen += std::to_string(curr->val);
309 gen +=
":ref [arrowhead=vee, arrowtail=dot, dir=both];";
319#ifdef LINKED_LIST_VISUALIZATION_H
321 std::string generated = this->generate();
322 linked_list_visualization::visualize(generated);
331 std::shared_ptr<node> curr_root;
339 explicit Iterator(
const std::shared_ptr<node> &l) noexcept : curr_root(l) {}
348 this->curr_root = current;
359 curr_root = curr_root->next;
382 curr_root = curr_root->prev;
Iterator class.
Definition doubly_linked_list.h:329
Iterator operator--(int)
operator – for type iterator
Definition doubly_linked_list.h:392
Iterator operator++(int)
operator ++ for type Iterator
Definition doubly_linked_list.h:369
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition doubly_linked_list.h:347
Iterator(const std::shared_ptr< node > &l) noexcept
Construct a new Iterator object.
Definition doubly_linked_list.h:339
Iterator & operator--()
operator – for type Iterator
Definition doubly_linked_list.h:380
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition doubly_linked_list.h:405
T operator*()
operator * for type Iterator
Definition doubly_linked_list.h:412
Iterator & operator++()
operator ++ for type Iterator
Definition doubly_linked_list.h:357
doubly linked list class
Definition doubly_linked_list.h:19
Iterator end()
pointer that points to end
Definition doubly_linked_list.h:83
void push_back(T key)
push_back function.
Definition doubly_linked_list.h:175
doubly_linked_list & operator=(const doubly_linked_list &l)
operator = for doubly linked list class
Definition doubly_linked_list.h:50
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:61
size_t size()
size function. Returns the size of the list.
Definition doubly_linked_list.h:67
void push_front(T key)
push_front function.
Definition doubly_linked_list.h:189
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:241
Iterator begin()
pointer that points to begin
Definition doubly_linked_list.h:76
friend std::ostream & operator<<(std::ostream &out, doubly_linked_list< T > &l)
<< operator for the doubly_linked_list class.
Definition doubly_linked_list.h:131
bool search(T key)
search function.
Definition doubly_linked_list.h:162
std::vector< T > elements()
elements function.
Definition doubly_linked_list.h:228
void erase(T key)
erase function.
Definition doubly_linked_list.h:203