AlgoPlus v0.1.0
Loading...
Searching...
No Matches
stack_list.h
1#ifndef STACK_H
2#define STACK_H
3
4#ifdef __cplusplus
5#include <iostream>
6#include <memory>
7#include <vector>
8#endif
9
13template <typename T> class stack_list {
14 private:
21 struct node {
22 T val;
23 std::shared_ptr<node> next;
24 std::shared_ptr<node> prev;
25 node(T key) : val(key), next(nullptr), prev(nullptr) {}
26 };
27
28 std::shared_ptr<node> root;
29 size_t _size{0};
30
31 public:
37 inline explicit stack_list(std::vector<T> v = {}) noexcept : root(nullptr) {
38 if (!v.empty()) {
39 for (T& x : v) {
40 this->push(x);
41 }
42 }
43 }
44
49 inline explicit stack_list(const stack_list& s) : root(s.root), _size(s._size) {}
50
56 inline stack_list& operator=(const stack_list& s) {
57 root = s.root;
58 _size = s._size;
59 return *this;
60 }
61
65 inline void clear() {
66 root = nullptr;
67 _size = 0;
68 }
69
75 inline size_t size() { return _size; }
76
82 inline void push(T key) {
83 std::shared_ptr<node> nn = std::make_shared<node>(key);
84 if (!root) {
85 root = nn;
86 _size++;
87 return;
88 } else {
89 root->next = nn;
90 nn->prev = root;
91 root = nn;
92 _size++;
93 }
94 }
95
101 inline T top() { return root->val; }
102
107 inline void pop() {
108 root = root->prev;
109 root->next = nullptr;
110 _size--;
111 }
112
113 class Iterator;
114
120 inline Iterator begin() { return Iterator(root); }
121
127 inline Iterator end() { return Iterator(nullptr); }
128};
129
133template <typename T> class stack_list<T>::Iterator {
134 private:
135 std::shared_ptr<node> curr_root;
136
137 public:
143 explicit Iterator(const std::shared_ptr<node>& s) noexcept : curr_root(s) {}
144
151 Iterator& operator=(std::shared_ptr<node> current) {
152 this->curr_root = current;
153 return *(this);
154 }
155
162 if (curr_root) {
163 curr_root = curr_root->prev;
164 }
165 return *(this);
166 }
167
174 Iterator it = *this;
175 ++*(this);
176 return it;
177 }
178
186 bool operator!=(const Iterator& it) { return curr_root != it.curr_root; }
187
193 T operator*() { return curr_root->val; }
194};
195
196#endif
Iterator class.
Definition stack_list.h:133
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition stack_list.h:186
Iterator operator++(int)
operator ++ for type Iterator
Definition stack_list.h:173
Iterator & operator++()
operator ++ for type Iterator
Definition stack_list.h:161
Iterator(const std::shared_ptr< node > &s) noexcept
Construct a new Iterator object.
Definition stack_list.h:143
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition stack_list.h:151
T operator*()
operator * for type Iterator
Definition stack_list.h:193
T top()
top function
Definition stack_list.h:101
void pop()
pop function removes the top of the stack
Definition stack_list.h:107
size_t size()
size functon
Definition stack_list.h:75
stack_list(std::vector< T > v={}) noexcept
Construct a new stack list object.
Definition stack_list.h:37
void push(T key)
push function
Definition stack_list.h:82
Iterator end()
pointer to the end of the stack
Definition stack_list.h:127
stack_list & operator=(const stack_list &s)
operator = for stack list class
Definition stack_list.h:56
Iterator begin()
pointer to the top of the stack
Definition stack_list.h:120
stack_list(const stack_list &s)
Copy constructor for stack list class.
Definition stack_list.h:49
void clear()
clear function
Definition stack_list.h:65