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 explicit stack_list(std::vector<T> v = {}) noexcept
38 : root(nullptr) {
39 if (!v.empty()) {
40 for (T &x : v) {
41 this->push(x);
42 }
43 }
44 }
45
50 explicit stack_list(const stack_list &s) : root(s.root), _size(s._size) {
51
52
53 }
54
61 root = s.root;
62 _size = s._size;
63 return *this;
64 }
65
69 void clear() {
70 root = nullptr;
71 _size = 0;
72 }
73
79 size_t size() { return _size; }
80
86 void push(T key) {
87 std::shared_ptr<node> nn = std::make_shared<node>(key);
88 if (!root) {
89 root = nn;
90 _size++;
91 return;
92 } else {
93 root->next = nn;
94 nn->prev = root;
95 root = nn;
96 _size++;
97 }
98 }
99
105 T top() { return root->val; }
106
111 void pop() {
112 root = root->prev;
113 root->next = nullptr;
114 _size--;
115 }
116
117 class Iterator;
118
124 Iterator begin() { return Iterator(root); }
125
131 Iterator end() { return Iterator(nullptr); }
132};
133
137template <typename T> class stack_list<T>::Iterator {
138 private:
139 std::shared_ptr<node> curr_root;
140
141 public:
147 explicit Iterator(const std::shared_ptr<node> &s) noexcept : curr_root(s) {}
148
155 Iterator &operator=(std::shared_ptr<node> current) {
156 this->curr_root = current;
157 return *(this);
158 }
159
166 if (curr_root) {
167 curr_root = curr_root->prev;
168 }
169 return *(this);
170 }
171
178 Iterator it = *this;
179 ++*(this);
180 return it;
181 }
182
190 bool operator!=(const Iterator &it) { return curr_root != it.curr_root; }
191
197 T operator*() { return curr_root->val; }
198};
199
200#endif
Iterator class.
Definition stack_list.h:137
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition stack_list.h:190
Iterator operator++(int)
operator ++ for type Iterator
Definition stack_list.h:177
Iterator & operator++()
operator ++ for type Iterator
Definition stack_list.h:165
Iterator(const std::shared_ptr< node > &s) noexcept
Construct a new Iterator object.
Definition stack_list.h:147
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type
Definition stack_list.h:155
T operator*()
operator * for type Iterator
Definition stack_list.h:197
stack_list class
Definition stack_list.h:13
T top()
top function
Definition stack_list.h:105
void pop()
pop function removes the top of the stack
Definition stack_list.h:111
size_t size()
size functon
Definition stack_list.h:79
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:86
Iterator end()
pointer to the end of the stack
Definition stack_list.h:131
stack_list & operator=(const stack_list &s)
operator = for stack list class
Definition stack_list.h:60
Iterator begin()
pointer to the top of the stack
Definition stack_list.h:124
stack_list(const stack_list &s)
Copy constructor for stack list class.
Definition stack_list.h:50
void clear()
clear function
Definition stack_list.h:69