-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.hpp
More file actions
62 lines (50 loc) · 1.22 KB
/
stack.hpp
File metadata and controls
62 lines (50 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef STACK_HPP
#define STACK_HPP
#include <utility>
#include "node.hpp"
template<class T>
class Stack
{
typedef SimpleNode<T> _Node;
public:
Stack()
:m_top(nullptr),m_size(0) {}
~Stack() { this->clear(); }
bool empty() { return (this->m_top==nullptr); }
size_t size() { return this->m_size; };
void swap(Stack<T> &other)
{
std::swap(this->m_top, other.m_top);
std::swap(this->m_size, other.m_size);
}
void clear()
{
while(m_top != nullptr)
this->pop();
}
void push(T data)
{
_Node *newHead { new _Node(data, this->m_top) };
this->m_top = newHead;
this->m_size++;
}
void pop()
{
if (this->m_top != nullptr)
{
_Node *newHead { this->m_top->next };
delete m_top;
m_top = newHead;
this->m_size--;
}
}
T top()
{
if (this->m_top != nullptr) return this->m_top->data;
return T();
}
private:
_Node *m_top;
size_t m_size;
};
#endif //STACK_HPP