-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
executable file
·162 lines (146 loc) · 3.15 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef H_STACK
#define H_STACK
#define DEBUG
#include <assert.h>
#include <stdlib.h>
#include <new>
template <class T>
class Stack {
public:
Stack();
~Stack();
using value_type = T;
using size_type = size_t;
using reference = T&;
using const_reference = const T&;
void push(value_type value);
void pop();
const_reference top() const;
bool empty() const;
size_type size() const;
private:
static const size_type initSize = 4;
size_type bufferSize;
value_type* buffer;
size_type elementsCount;
void resizeBuffer(size_type new_size);
void is_OK() const;
void DUMP() const;
};
template <class T>
Stack<T>::Stack() : bufferSize(initSize), elementsCount(0) {
try {
buffer = new value_type[bufferSize];
#ifdef DEBUG
std::cout << "INIT STACK";
DUMP();
#endif
} catch (std::bad_alloc& e) {
std::cout << "allocation failed\n";
}
}
template <class T>
Stack<T>::~Stack() {
is_OK();
delete[] buffer;
#ifdef DEBUG
std::cout << "STACK IS DELETED";
#endif
}
//
template <class T>
void Stack<T>::push(value_type value) {
#ifdef DEBUG
std::cout << "call push\n";
#endif
is_OK();
try {
if (elementsCount == bufferSize) {
resizeBuffer(2 * bufferSize);
}
buffer[elementsCount] = value;
elementsCount++;
} catch (std::bad_alloc) {
std::cout << "not pushed\n";
throw;
return;
}
}
template <class T>
void Stack<T>::pop() {
#ifdef DEBUG
std::cout << "call pop\n";
#endif
is_OK();
try {
if (elementsCount == 0) throw - 1;
elementsCount--;
} catch (int) {
std::cout << "the stack is empty\n";
}
}
template <class T>
typename Stack<T>::const_reference Stack<T>::top() const {
is_OK();
try {
if (elementsCount == 0) throw - 1;
return buffer[elementsCount - 1];
} catch (int) {
std::cout << "the stack is empty\n";
}
}
template <class T>
bool Stack<T>::empty() const {
is_OK();
return elementsCount == 0;
}
template <class T>
typename Stack<T>::size_type Stack<T>::size() const {
is_OK();
return elementsCount;
}
template <class T>
void Stack<T>::resizeBuffer(typename Stack<T>::size_type new_size) {
#ifdef DEBUG
std::cout << "reallocation...\n";
#endif
is_OK();
if (new_size <= bufferSize) return;
try {
value_type* bufferCopy = new value_type[new_size];
for (size_type i = 0; i < bufferSize; i++) {
bufferCopy[i] = buffer[i];
}
delete[] buffer;
buffer = bufferCopy;
bufferSize = new_size;
} catch (std::bad_alloc) {
std::cout << "allocation failed\n";
throw;
return;
}
DUMP();
}
template <class T>
void Stack<T>::is_OK() const {
assert(elementsCount >= 0);
assert(bufferSize > 0);
assert(elementsCount <= bufferSize);
assert(buffer != NULL);
}
template <class T>
void Stack<T>::DUMP() const {
is_OK();
std::cout << "\n-----printing the stack:-----\n";
std::cout << "this: " << this << "\n";
std::cout << "bufferSize = " << bufferSize << "\n";
std::cout << size() << " elements, " << empty() << " empty\n";
if (!empty()) {
for (size_type i = 0; i < elementsCount; i++) {
std::cout << buffer[i] << " ";
}
std::cout << "\n";
}
std::cout << "-----stack is printed-----\n\n";
}
#endif