4#ifdef ENABLE_LINKED_LIST_VISUALIZATION
5#include "../../visualization/list_visual/linked_list_visualization.h"
27 inline explicit linked_list(std::vector<T> _elements = {})
noexcept
28 : root(nullptr), tail(nullptr) {
29 if (!_elements.empty()) {
30 for (T& x : _elements) {
41 : root(l.root), tail(l.tail), _size(l._size) {}
59 inline bool empty() {
return root ==
nullptr; }
65 inline size_t size() {
return _size; }
130 std::shared_ptr<node> head = l1.root;
132 out << head->val <<
' ';
147 std::shared_ptr<node> next;
148 node(T key = 0) : val(key), next(nullptr) {}
150 std::shared_ptr<node> root;
151 std::shared_ptr<node> tail;
154 std::string generate();
158 std::shared_ptr<node> p = std::make_shared<node>(key);
159 if (root ==
nullptr) {
169 std::shared_ptr<node> p = std::make_shared<node>(key);
171 if (tail ==
nullptr) {
182 std::shared_ptr<node> t = root;
183 std::shared_ptr<node> to_be_removed =
nullptr;
184 while (t != tail && t->next->val != key) {
190 to_be_removed = t->next;
191 t->next = t->next->next;
192 to_be_removed.reset();
193 if (t->next ==
nullptr) {
207 std::shared_ptr<node> t = root;
208 while (t != tail && t->val != key) {
211 if (t == tail || t ==
nullptr) {
216 }
catch (std::invalid_argument& e) {
217 std::cerr << e.what() <<
'\n';
223 std::vector<T> _elements;
228 std::shared_ptr<node> head = root;
230 _elements.push_back(head->val);
237 std::shared_ptr<node> current = root;
238 std::shared_ptr<node> prev{
nullptr}, next{
nullptr};
240 while (current !=
nullptr) {
241 next = current->next;
242 current->next = prev;
249template <
typename T>
inline std::string linked_list<T>::generate() {
251 gen +=
"rankdir=LR;";
253 gen +=
"node [shape=record;]";
255 std::vector<T> els = this->elements();
256 if (std::is_same_v<T, std::string> || std::is_same_v<T, char>) {
257 for (
auto& x : els) {
259 gen +=
" [label=<{ ";
265 std::shared_ptr<node> curr = root;
269 gen += curr->next->val;
270 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
275 for (
auto& x : els) {
276 gen += std::to_string(x);
277 gen +=
" [label=<{ ";
278 gen += std::to_string(x);
283 std::shared_ptr<node> curr = root;
285 gen += std::to_string(curr->val);
287 gen += std::to_string(curr->next->val);
288 gen +=
":data [arrowhead=vee, arrowtail=dot, dir=both];";
296#ifdef ENABLE_LINKED_LIST_VISUALIZATION
298 std::string generated = this->generate();
299 linked_list_visualization::visualize(generated);
308 std::shared_ptr<node> curr_root;
316 explicit Iterator(
const std::shared_ptr<node>& l) noexcept : curr_root(l) {}
325 this->curr_root = current;
336 curr_root = curr_root->next;
Iterator class.
Definition linked_list.h:306
Iterator(const std::shared_ptr< node > &l) noexcept
Construct a new Iterator object.
Definition linked_list.h:316
Iterator & operator++()
operator ++ for type Iterator
Definition linked_list.h:334
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition linked_list.h:324
T operator*()
operator * for type Iterator
Definition linked_list.h:366
Iterator operator++(int)
operator ++ for type Iterator
Definition linked_list.h:346
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition linked_list.h:359
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:168
void reverse()
reverse function.
Definition linked_list.h:236
bool search(T key)
search function.
Definition linked_list.h:202
Iterator begin()
pointer that points to begin
Definition linked_list.h:74
void erase(T key)
erase function.
Definition linked_list.h:178
void push_back(T key)
push_back function.
Definition linked_list.h:157
friend std::ostream & operator<<(std::ostream &out, linked_list< T > &l1)
<< operator for the linked_list class.
Definition linked_list.h:128
Iterator end()
pointer that points to end
Definition linked_list.h:81
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:59
size_t size()
size function. Returns the size of the list.
Definition linked_list.h:65
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:222
linked_list & operator=(const linked_list &l)
operator = for linked list class
Definition linked_list.h:48