1#ifndef CIRCULAR_LINKED_LIST_H
2#define CIRCULAR_LINKED_LIST_H
4#ifdef ENABLE_LIST_VISUALIZATION
5#include "../../visualization/list_visual/linked_list_visualization.h"
26 : root(nullptr), tail(nullptr) {
27 if (!_elements.empty()) {
28 for (T& x : _elements) {
39 : root(c.root), tail(c.tail), _size(c._size) {}
59 inline bool empty() {
return root ==
nullptr; }
66 inline size_t size() {
return _size; }
132 std::shared_ptr<node> head = l1.root;
134 out << head->val <<
' ';
136 }
while (head != l1.root);
149 std::shared_ptr<node> next;
150 node(T key = 0) : val(key), next(nullptr) {}
153 std::shared_ptr<node> root;
154 std::shared_ptr<node> tail;
157 std::string generate();
161 std::shared_ptr<node> p = std::make_shared<node>(key);
162 if (root ==
nullptr) {
174 std::shared_ptr<node> p = std::make_shared<node>(key);
175 if (root ==
nullptr) {
191 std::shared_ptr<node> t = root;
192 std::shared_ptr<node> to_be_removed =
nullptr;
195 if (t->next->val == key) {
196 to_be_removed = t->next;
197 t->next = t->next->next;
198 to_be_removed.reset();
201 if (t->next == root) {
221 std::shared_ptr<node> t = root;
231 }
catch (std::invalid_argument& e) {
232 std::cerr << e.what() <<
'\n';
238 std::vector<T> _elements;
244 std::shared_ptr<node> head = root;
246 _elements.push_back(head->val);
248 }
while (head != root);
253template <
typename T>
inline std::string circular_linked_list<T>::generate() {
255 gen +=
"rankdir=LR;";
257 gen +=
"node [shape=record;]";
259 std::vector<T> els = this->elements();
260 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
261 for (
auto& x : els) {
263 gen +=
" [label=<{ ";
269 std::shared_ptr<node> curr = root;
273 gen += curr->next->val;
274 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
282 for (
auto& x : els) {
283 gen += std::to_string(x);
284 gen +=
" [label=<{ ";
285 gen += std::to_string(x);
290 std::shared_ptr<node> curr = root;
292 gen += std::to_string(curr->val);
294 gen += std::to_string(curr->next->val);
295 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
306#ifdef ENABLE_LIST_VISUALIZATION
308 std::string generated = this->generate();
309 linked_list_visualization::visualize(generated);
318 std::shared_ptr<node> curr_root;
326 explicit Iterator(
const std::shared_ptr<node>& l) noexcept : curr_root(l) {}
335 this->curr_root = current;
346 curr_root = curr_root->next;
Iterator class.
Definition circular_linked_list.h:316
Iterator & operator++()
operator ++ for type Iterator
Definition circular_linked_list.h:344
T operator*()
operator * for type Iterator
Definition circular_linked_list.h:376
Iterator(const std::shared_ptr< node > &l) noexcept
Construct a new Iterator object.
Definition circular_linked_list.h:326
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition circular_linked_list.h:334
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition circular_linked_list.h:369
Iterator operator++(int)
operator ++ for type Iterator
Definition circular_linked_list.h:356
bool search(T key)
search function
Definition circular_linked_list.h:216
void push_back(T key)
push_back function
Definition circular_linked_list.h:160
Iterator begin()
pointer that points to begin
Definition circular_linked_list.h:75
std::vector< T > elements()
elements function
Definition circular_linked_list.h:237
void erase(T key)
erase function
Definition circular_linked_list.h:186
void push_front(T key)
push_front function
Definition circular_linked_list.h:173
circular_linked_list & operator=(const circular_linked_list &c)
operator = for circular linked list class
Definition circular_linked_list.h:46
Iterator end()
pointer that points to end
Definition circular_linked_list.h:82
friend std::ostream & operator<<(std::ostream &out, circular_linked_list< T > &l1)
<< operator for the circular list class
Definition circular_linked_list.h:130
circular_linked_list(const circular_linked_list &c)
copy constructor for the circular linked list class
Definition circular_linked_list.h:38
size_t size()
size function
Definition circular_linked_list.h:66
void visualize()
visualize function returns a .dot file that can be previewd with graphviz plugin in vscode
bool empty()
empty function
Definition circular_linked_list.h:59
circular_linked_list(std::vector< T > _elements={}) noexcept
Construct a new circular linked list object.
Definition circular_linked_list.h:25