forked from HPI-Artificial-Intelligence-Teaching/24-pt2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
123 lines (100 loc) · 2.97 KB
/
stack.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/******************************************************************************
*
* A generic stack using a resize-able array.
*
* Based on the source code from Robert Sedgewick and Kevin Wayne at https://algs4.cs.princeton.edu/
*
******************************************************************************/
#ifndef __STACK_H__
#define __STACK_H__
#include <stdexcept>
// Implements a stack with a fixed size array of values that will be resized if more capacity is needed
template <typename T>
class Stack {
T* stack_data;
int n;
int capacity;
// internal function that resizes the stack to a target size
void resize(int new_capacity) {
// allocate new array
T* new_stack_data = new T[new_capacity];
// copy all elements on the stack to the new stack
for (auto i = 0; i < n; i++)
new_stack_data[i] = stack_data[i];
// remove old stack and move pointer
delete[] stack_data;
stack_data = new_stack_data;
// set new capacity
capacity = new_capacity;
return;
}
public:
// constructor
Stack(int cap = 1) : n(0), capacity(cap) {
stack_data = new T[capacity];
}
// copy constructor
Stack(const Stack& s) : n(s.n), capacity(s.capacity) {
stack_data = new T[capacity];
for (auto i = 0; i < s.n; i++)
stack_data[i] = s.stack_data[i];
}
// move constructor
Stack(Stack&& s) : n(s.n), capacity(s.capacity) {
stack_data = s.stack_data;
s.stack_data = nullptr;
}
// copy assignment
Stack& operator=(const Stack& s) {
// free the existing stack
delete[] stack_data;
// copy the stack passed in
n = s.n;
capacity = s.capacity;
stack_data = new T[capacity];
for (auto i = 0; i < s.n; i++)
stack_data[i] = s.stack_data[i];
return (*this);
}
// move assignment
Stack& operator=(Stack&& s) {
// free the existing stack
delete[] stack_data;
n = s.n;
capacity = s.capacity;
stack_data = s.stack_data;
s.stack_data = nullptr;
return (*this);
}
// destructor
~Stack() {
delete[] stack_data;
}
// is_empty method
const bool is_empty() const {
return (n == 0);
}
// push method
void push(const T& x) {
// check if the size is exceeded and then resize
if (n == capacity)
resize(2 * capacity);
stack_data[n++] = x;
}
// pop method
T& pop() {
if (is_empty())
throw std::logic_error("Calling pop on an empty stack");
// check if we can shrink the stack
if (n == capacity / 4)
resize(capacity / 2);
return (stack_data[--n]);
}
// top method
const T& top() const {
if (is_empty())
throw std::logic_error("Calling top on an empty stack");
return (stack_data[n - 1]);
}
};
#endif