DSA - Data Structures and Algorithms
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1
11
12#ifndef STACK_H
13#define STACK_H
14
15#include "list.h"
16
17#include <initializer_list>
18#include <iostream>
19#include <utility>
20
21namespace dsa
22{
23
24 template<typename T>
25 class Stack;
26
27 template<typename T>
28 auto operator==(const Stack<T>& stack1, const Stack<T>& stack2) -> bool;
29
30 template<typename T>
31 auto operator<(const Stack<T>& stack1, const Stack<T>& stack2) -> bool;
32
42 template<typename T>
43 class Stack
44 {
45 public:
46
52 using value_type = T;
53
59 using reference = T&;
60
66 using const_reference = const T&;
67
72 Stack() = default;
73
79 Stack(const std::initializer_list<T>& init_list);
80
86 Stack(const Stack<T>& other);
87
94 auto operator=(const Stack<T>& other) -> Stack&;
95
102 Stack(Stack<T>&& other) noexcept;
103
111 auto operator=(Stack<T>&& other) noexcept -> Stack&;
112
116 ~Stack() = default;
117
123 auto top() -> reference;
124
130 [[nodiscard]] auto top() const -> const_reference;
131
138 [[nodiscard]] auto empty() const -> bool;
139
145 [[nodiscard]] auto size() const -> size_t;
146
152 void push(T value);
153
157 void pop();
158
164 void swap(Stack<T>& other) noexcept;
165
166 private:
167
171 friend auto operator==<T>(const Stack<T>& stack1, const Stack<T>& stack2) -> bool;
172
176 friend auto operator< <T>(const Stack<T>& stack1, const Stack<T>& stack2) -> bool;
177
178 List<T> container{};
179 };
180
181 template<typename T>
182 Stack<T>::Stack(const std::initializer_list<T>& init_list)
183 {
184 for (const auto& item : init_list)
185 {
186 container.push_back(item);
187 }
188 }
189
190 template<typename T>
192 {
193 if (other.size() >= 1)
194 {
195 for (const auto& item : other.container)
196 {
197 container.push_back(item);
198 }
199 }
200 }
201
202 template<typename T>
203 auto Stack<T>::operator=(const Stack<T>& other) -> Stack<T>&
204 {
205 if (&other != this)
206 {
207 while (!container.empty())
208 {
209 container.pop_front();
210 }
211
212 for (const auto& item : other.container)
213 {
214 container.push_back(item);
215 }
216 }
217
218 return *this;
219 }
220
221 template<typename T>
222 Stack<T>::Stack(Stack<T>&& other) noexcept
223 {
224 std::swap(container, other.container);
225 }
226
227 template<typename T>
228 auto Stack<T>::operator=(Stack<T>&& other) noexcept -> Stack<T>&
229 {
230 if (&other != this)
231 {
232 std::swap(container, other.container);
233 }
234
235 return *this;
236 }
237
238 template<typename T>
240 {
241 return container.back();
242 }
243
244 template<typename T>
245 auto Stack<T>::top() const -> typename Stack<T>::const_reference
246 {
247 return container.back();
248 }
249
250 template<typename T>
251 auto Stack<T>::empty() const -> bool
252 {
253 return container.size() == 0;
254 }
255
256 template<typename T>
257 auto Stack<T>::size() const -> size_t
258 {
259 return container.size();
260 }
261
262 template<typename T>
263 void Stack<T>::push(T value)
264 {
265 container.push_back(value);
266 }
267
268 template<typename T>
270 {
271 container.pop_back();
272 }
273
274 template<typename T>
275 void Stack<T>::swap(Stack<T>& other) noexcept
276 {
277 if (&other != this)
278 {
279 std::swap(container, other.container);
280 }
281 }
282
291 template<typename T>
292 auto operator<<(std::ostream& out, const Stack<T>& stack) -> std::ostream&
293 {
294 Stack<T> temp{ stack };
295
296 while (!temp.empty())
297 {
298 out << temp.top() << ' ';
299 temp.pop();
300 }
301
302 return out;
303 }
304
314 template<typename T>
315 auto operator==(const Stack<T>& stack1, const Stack<T>& stack2) -> bool
316 {
317 return stack1.container == stack2.container;
318 }
319
329 template<typename T>
330 auto operator!=(const Stack<T>& stack1, const Stack<T>& stack2) -> bool
331 {
332 return !(operator==(stack1, stack2));
333 }
334
345 template<typename T>
346 auto operator<(const Stack<T>& stack1, const Stack<T>& stack2) -> bool
347 {
348 return stack1.container < stack2.container;
349 }
350
361 template<typename T>
362 auto operator>(const Stack<T>& stack1, const Stack<T>& stack2) -> bool
363 {
364 return operator<(stack2, stack1);
365 }
366
377 template<typename T>
378 auto operator<=(const Stack<T>& stack1, const Stack<T>& stack2) -> bool
379 {
380 return !(operator>(stack1, stack2));
381 }
382
393 template<typename T>
394 auto operator>=(const Stack<T>& stack1, const Stack<T>& stack2) -> bool
395 {
396 return !(operator<(stack1, stack2));
397 }
398
399}
400#endif // !STACK_H
constexpr auto operator==(const Array< T, N > &lhs, const Array< T, N > &rhs) noexcept(noexcept(*lhs.begin()== *rhs.begin())) -> bool
The relational operator compares two Array objects.
Definition array.h:591
auto operator<<(std::ostream &out, const Array< T, N > &array) -> std::ostream &
Overloads operator to print all elements of Array.
Definition array.h:572
Implements List using Node with pointer to adjacent elements as internal base.
Definition list.h:57
Implements Stack class.
Definition stack.h:44
T & reference
Alias for reference to data type used in class.
Definition stack.h:59
T value_type
Alias for data type used in class.
Definition stack.h:52
void swap(Stack< T > &other) noexcept
Function swaps content of two Stack objects.
Definition stack.h:275
const T & const_reference
Alias for const reference to data type used in class.
Definition stack.h:66
void push(T value)
Function add new element at the top of Stack.
Definition stack.h:263
~Stack()=default
Destroy the Stack object.
auto operator=(const Stack< T > &other) -> Stack &
Constructs Stack using copy assignment.
Definition stack.h:203
friend auto operator<(const Stack< T > &stack1, const Stack< T > &stack2) -> bool
Forward friend declaration to access internal container comparison operator.
Definition stack.h:346
void pop()
Function removes the top element of Stack.
Definition stack.h:269
auto size() const -> size_t
Function returns Stack size.
Definition stack.h:257
Stack()=default
Construct a new Stack object.
friend auto operator==(const Stack< T > &stack1, const Stack< T > &stack2) -> bool
Forward friend declaration to access internal container comparison operator.
Definition stack.h:315
auto top() -> reference
Function returns pointer to Stack top element.
Definition stack.h:239
auto empty() const -> bool
Function checks if container has no elements.
Definition stack.h:251
This file contains implementation of List class.
auto operator>(const List< T > &list1, const List< T > &list2) -> bool
The relational operator compares two List objects.
Definition list.h:1980
auto operator>=(const List< T > &list1, const List< T > &list2) -> bool
The relational operator compares two List objects.
Definition list.h:2012
auto operator<=(const List< T > &list1, const List< T > &list2) -> bool
The relational operator compares two List objects.
Definition list.h:1996
auto operator!=(const List< T > &list1, const List< T > &list2) -> bool
The relational operator compares two List objects.
Definition list.h:1928
auto operator<(const List< T > &list1, const List< T > &list2) -> bool
The relational operator compares two List objects.
Definition list.h:1944