-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.cpp
62 lines (54 loc) · 1.29 KB
/
3.cpp
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
/*************************************************************************
> File Name: 3.cpp
> Author: Louis1992
> Mail: zhenchaogan@gmail.com
> Blog: http://gzc.github.io
> Created Time: Mon Nov 9 12:48:21 2015
************************************************************************/
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
class clusteredStacks {
int N; //maximum capacity for every stack
vector<stack<int>> mystacks;
int cur;
public:
clusteredStacks(int n) : N(n) {
cur = -1;
}
void push(int v) {
if(mystacks.size() > 0 && mystacks[cur].size() < N) {
mystacks[cur].push(v);
} else {
stack<int> temp;
temp.push(v);
mystacks.push_back(temp);
cur++;
}
}
int top() {
return mystacks[cur].top();
}
void pop() {
mystacks[cur].pop();
if(mystacks[cur].empty()) {
cur--;
mystacks.pop_back();
}
}
bool empty() {
return cur == -1;
}
};
int main() {
clusteredStacks stack(2);
stack.push(1);
stack.push(2);
stack.push(3);
while(!stack.empty()) {
cout << stack.top() << endl;
stack.pop();
}
return 0;
}