-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathststack.cpp
More file actions
48 lines (38 loc) · 840 Bytes
/
Copy pathststack.cpp
File metadata and controls
48 lines (38 loc) · 840 Bytes
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
#include "ststack.h"
ststack::ststack() : topindex(-1), capacity(10) {
arr = new char[capacity];
}
ststack::~ststack() {
delete[] arr;
}
void ststack::resize() {
capacity *= 2;
char* newArr = new char[capacity];
//realloc
for (int i = 0; i <= topindex; i++) {
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
}
void ststack::push(char x) {
if (topindex + 1 == capacity) {
resize(); // Óâåëè÷èâàåì ðàçìåð ìàññèâà ïðè íåîáõîäèìîñòè
}
arr[++topindex] = x;
}
char ststack::pop() {
if (isEmpty()) {
return 0; // Âîçâðàùàåì 0, åñëè ñòåê ïóñò
}
return arr[topindex--];
}
char ststack::top() {
if (isEmpty()) {
return 0; // Âîçâðàùàåì 0, åñëè ñòåê ïóñò
}
return arr[topindex];
}
bool ststack::isEmpty() {
return topindex == -1;
}