-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdstack.cpp
More file actions
72 lines (54 loc) · 1001 Bytes
/
dstack.cpp
File metadata and controls
72 lines (54 loc) · 1001 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstdlib>
using namespace std;
class Stack{
const static int chunk_ = 2;
int *data_;
int size_;
int top_;
public:
Stack() : top_(-1), size_(chunk_), data_(new int[chunk_]) {}
~Stack(){
cout << "destructing" << endl;
delete []data_;
}
void push(int v) {
if(top_ == (size_ - 1)){
resize();
}
data_[++top_] = v;
}
int pop(void) {
if (top_ < 0 ){
throw exception();
}
return data_[top_--];
}
private:
void resize(){
int* tmp = data_;
data_ = new int[size_ + chunk_];
for(int i = 0; i < size_; i++){
data_[i] = tmp[i];
}
delete [] tmp;
cout << "resized" << endl;
}
};
int main() {
Stack st;
st.push (1);
cout << "push 1" << endl;
st.push (2);
cout << "push 2" << endl;
st.push (3);
cout << "push 3" << endl;
try {
cout << st.pop () << endl;
cout << st.pop () << endl;
cout << st.pop () << endl;
} catch( const exception & e) {
cout << "basi feila" << endl;
}
return 0;
}