4#ifdef LINKED_LIST_VISUALIZATION_H
5#include "../../visualization/list_visual/linked_list_visualization.h"
28 : root(nullptr), tail(nullptr) {
29 if (!_elements.empty()) {
30 for (T &x : _elements) {
62 bool empty() {
return root ==
nullptr; }
68 size_t size() {
return _size; }
90 void push_back(T key);
96 void push_front(T key);
115 std::vector<T> elements();
133 std::shared_ptr<node> head = l1.root;
135 out << head->val <<
' ';
150 std::shared_ptr<node> next;
151 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) {
172 std::shared_ptr<node> p = std::make_shared<node>(key);
174 if(tail ==
nullptr) { tail = root; }
183 std::shared_ptr<node> t = root;
184 std::shared_ptr<node> to_be_removed =
nullptr;
185 while (t != tail && t->next->val != key) {
191 to_be_removed = t->next;
192 t->next = t->next->next;
193 to_be_removed.reset();
194 if (t->next ==
nullptr) {
208 std::shared_ptr<node> t = root;
209 while (t != tail && t->val != key) {
212 if (t == tail || t ==
nullptr) {
217 }
catch (std::invalid_argument &e) {
218 std::cerr << e.what() <<
'\n';
224 std::vector<T> _elements;
229 std::shared_ptr<node> head = root;
231 _elements.push_back(head->val);
238 std::shared_ptr<node> current = root;
239 std::shared_ptr<node> prev{
nullptr}, next{
nullptr};
241 while (current !=
nullptr) {
242 next = current->next;
243 current->next = prev;
252 gen +=
"rankdir=LR;";
254 gen +=
"node [shape=record;]";
256 std::vector<T> els = this->elements();
257 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
258 for (
auto &x : els) {
260 gen +=
" [label=<{ ";
266 std::shared_ptr<node> curr = root;
270 gen += curr->next->val;
271 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
276 for (
auto &x : els) {
277 gen += std::to_string(x);
278 gen +=
" [label=<{ ";
279 gen += std::to_string(x);
284 std::shared_ptr<node> curr = root;
286 gen += std::to_string(curr->val);
288 gen += std::to_string(curr->next->val);
289 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
297#ifdef LINKED_LIST_VISUALIZATION_H
299 std::string generated = this->generate();
300 linked_list_visualization::visualize(generated);
310 std::shared_ptr<node> curr_root;
318 explicit Iterator(
const std::shared_ptr<node> &l) noexcept : curr_root(l) {}
327 this->curr_root = current;
338 curr_root = curr_root->next;
Iterator class.
Definition linked_list.h:308
Iterator(const std::shared_ptr< node > &l) noexcept
Construct a new Iterator object.
Definition linked_list.h:318
Iterator & operator++()
operator ++ for type Iterator
Definition linked_list.h:336
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition linked_list.h:326
T operator*()
operator * for type Iterator
Definition linked_list.h:368
Iterator operator++(int)
operator ++ for type Iterator
Definition linked_list.h:348
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition linked_list.h:361
single linked list class
Definition linked_list.h:20
linked_list(const linked_list &l)
copy constructor for the linked_list class
Definition linked_list.h:40
void push_front(T key)
push_front function.
Definition linked_list.h:171
void reverse()
reverse function.
Definition linked_list.h:237
bool search(T key)
search function.
Definition linked_list.h:203
Iterator begin()
pointer that points to begin
Definition linked_list.h:77
void erase(T key)
erase function.
Definition linked_list.h:179
void push_back(T key)
push_back function.
Definition linked_list.h:160
friend std::ostream & operator<<(std::ostream &out, linked_list< T > &l1)
<< operator for the linked_list class.
Definition linked_list.h:131
Iterator end()
pointer that points to end
Definition linked_list.h:84
void visualize()
visualize function returns a .dot file that can be previewd with graphviz plugin in vscode
bool empty()
empty function. Returns true if the list is empty.
Definition linked_list.h:62
size_t size()
size function. Returns the size of the list.
Definition linked_list.h:68
linked_list(std::vector< T > _elements={}) noexcept
linked_list class constructor
Definition linked_list.h:27
std::vector< T > elements()
elements function.
Definition linked_list.h:223
linked_list & operator=(const linked_list &l)
operator = for linked list class
Definition linked_list.h:51