-
Notifications
You must be signed in to change notification settings - Fork 1
/
int_stack.cc
46 lines (38 loc) · 868 Bytes
/
int_stack.cc
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
#include "int_stack.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int IntStack::kInitialCapacity = 8;
IntStack::IntStack()
: a_((int*)malloc(kInitialCapacity * sizeof(int))),
capacity_(kInitialCapacity),
size_(0) {
}
IntStack::~IntStack() {
free(a_);
}
void IntStack::Push(int c) {
if (size_ == capacity_) {
int* b = static_cast<int*>(malloc(capacity_ * 2 * sizeof(int)));
memcpy(b, a_, capacity_ * sizeof(int));
free(a_);
a_ = b;
capacity_ *= 2;
}
a_[size_++] = c;
}
int IntStack::Peek() {
if (size_ == 0) {
fprintf(stderr, "error: Try to Peek() againt an empty stack.\n");
exit(1);
}
return a_[size_ - 1];
}
int IntStack::Pop() {
if (size_ == 0) {
fprintf(stderr, "error: Try to Pop() againt an empty stack.\n");
exit(1);
}
return a_[--size_];
}