-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement_stack.cpp
More file actions
174 lines (128 loc) · 3.17 KB
/
Copy pathimplement_stack.cpp
File metadata and controls
174 lines (128 loc) · 3.17 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
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
163
164
165
166
167
168
169
170
171
172
173
174
#include <bits/stdc++.h>
using namespace std;
class IntStack {
public:
IntStack();
~IntStack();
void push(int num);
void pop();
int& top();
int size() const;
int capacity() const;
bool empty() const;
void clear();
void reserve(int new_capacity);
private:
int *data;
int curr_capacity;
int curr_size;
};
IntStack::IntStack() : data(nullptr), curr_size(0), curr_capacity(0){}
IntStack::~IntStack() {
delete[] data;
}
void IntStack::push(int num) {
if (curr_capacity == curr_size) {
if (curr_capacity == 0) {
reserve(1);
} else {
reserve(curr_capacity * 2);
}
}
data[curr_size] = num;
curr_size++;
}
void IntStack::pop() {
if (curr_size == 0) return;
curr_size--;
}
int& IntStack::top() {
if (curr_size == 0) {
throw out_of_range("stack is empty");
}
return data[curr_size-1];
}
int IntStack::size() const{
return curr_size;
}
int IntStack::capacity() const{
return curr_capacity;
}
bool IntStack::empty() const{
return curr_size == 0;
}
void IntStack::clear() {
curr_size = 0;
}
void IntStack::reserve(int new_capacity) {
if (new_capacity <= curr_capacity) return;
int *new_data = new int[new_capacity];
for (int i=0; i<curr_size; i++) {
new_data[i] = data[i];
}
delete[] data;
data = new_data;
curr_capacity = new_capacity;
}
int main() {
IntStack stack;
assert(stack.empty());
assert(stack.size() == 0);
assert(stack.capacity() == 0);
stack.push(10);
assert(!stack.empty());
assert(stack.size() == 1);
assert(stack.capacity() == 1);
assert(stack.top() == 10);
stack.push(20);
assert(stack.size() == 2);
assert(stack.capacity() == 2);
assert(stack.top() == 20);
stack.push(30);
assert(stack.size() == 3);
assert(stack.capacity() == 4);
assert(stack.top() == 30);
// top이 참조를 반환하는지 확인
stack.top() = 300;
assert(stack.top() == 300);
assert(stack.size() == 3);
stack.pop();
assert(stack.size() == 2);
assert(stack.top() == 20);
stack.pop();
assert(stack.size() == 1);
assert(stack.top() == 10);
// reserve가 기존 값과 size를 유지하는지 확인
stack.reserve(20);
assert(stack.capacity() == 20);
assert(stack.size() == 1);
assert(stack.top() == 10);
// 더 작은 reserve는 무시
stack.reserve(5);
assert(stack.capacity() == 20);
assert(stack.size() == 1);
// clear는 capacity를 유지
stack.clear();
assert(stack.empty());
assert(stack.size() == 0);
assert(stack.capacity() == 20);
// 빈 스택 pop은 아무 일도 하지 않음
stack.pop();
assert(stack.size() == 0);
// 빈 스택 top은 예외
bool exceptionThrown = false;
try {
stack.top();
} catch (const out_of_range&) {
exceptionThrown = true;
}
assert(exceptionThrown);
// clear 이후 재사용
stack.push(100);
stack.push(200);
assert(stack.size() == 2);
assert(stack.capacity() == 20);
assert(stack.top() == 200);
cout << "ALL TESTS PASSED\n";
return 0;
}