-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.h
More file actions
105 lines (86 loc) · 1.63 KB
/
Stack.h
File metadata and controls
105 lines (86 loc) · 1.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by nikstarling on 2/16/20.
//
#ifndef PARSER_STACK_H
#define PARSER_STACK_H
template <class T>
class Stack {
private:
int cursor;
int size;
T *data;
public:
Stack(int size);
Stack(int size, T base_value);
~Stack();
Stack(const Stack& stack );
void push(T value);
T last();
T pop();
int get_size();
T operator[](int i);
};
template <class T>
Stack<T>::Stack(const Stack& stack)
{
this->cursor = stack.cursor;
this->size = stack.size;
this->data = new T[this->size];
for(int i = 0; i < this->size; ++i)
this->data[i] = stack.data[i];
}
template <class T>
T Stack<T>::operator[](int i)
{
return this->data[i];
}
template <class T>
int Stack<T>::get_size()
{
return this->cursor;
}
template <class T>
Stack<T>::Stack(int size)
{
this->size = size;
this->data = new T[size];
this->cursor = 0;
}
template <class T>
Stack<T>::Stack(int size, T base_value)
{
this->size = size;
this->data = new T[size];
this->cursor = 0;
for(int i = 0; i < size; ++i)
this->data = base_value;
}
template <class T>
Stack<T>::~Stack()
{
delete[] this->data;
}
template <class T>
void Stack<T>::push(T value)
{
if(this->cursor < this->size)
this->data[this->cursor++] = value;
}
template <class T>
T Stack<T>::last()
{
return this->data[this->cursor - 1];
}
template <class T>
T Stack<T>::pop()
{
return this->data[--this->cursor];
}
template <class T>
void print(Stack<T> stack)
{
for(int i = 0; i < stack.get_size(); ++i)
std::cout << stack[i] << " ";
std::cout << std::endl;
}
#endif //PARSER_STACK_H