-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.1.cpp
96 lines (89 loc) · 1.94 KB
/
3.1.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
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
#include <iostream>
using namespace std;
class stack3{
public:
stack3(int size = 300){
buf = new int[size*3];
ptop[0]=ptop[1]=ptop[2]=-1;
this->size = size;
}
~stack3(){
delete[] buf;
}
void push(int stackNum, int val){
int idx = stackNum*size + ptop[stackNum] + 1;
buf[idx] = val;
++ptop[stackNum];
}
void pop(int stackNum){
--ptop[stackNum];
}
int top(int stackNum){
int idx = stackNum*size + ptop[stackNum];
return buf[idx];
}
bool empty(int stackNum){
return ptop[stackNum]==-1;
}
private:
int *buf;
int ptop[3];
int size;
};
typedef struct node{
int val,preIdx;
}node;
class stack3_1{
public:
stack3_1(int totalSize = 900){
buf = new node[totalSize];
ptop[0]=ptop[1]=ptop[2]=-1;
this->totalSize = totalSize;
cur = 0;
}
~stack3_1(){
delete[] buf;
}
void push(int stackNum, int val){
buf[cur].val = val;
buf[cur].preIdx = ptop[stackNum];
ptop[stackNum] = cur;
++cur;
}
void pop(int stackNum){
ptop[stackNum] = buf[ptop[stackNum]].preIdx;
}
int top(int stackNum){
return buf[ptop[stackNum]].val;
}
bool empty(int stackNum){
return ptop[stackNum]==-1;
}
private:
node *buf;
int ptop[3];
int totalSize;
int cur;
};
int main(){
stack3_1 mystack;//stack3 mystack;
for(int i=0; i<10; ++i)
mystack.push(0, i);
for(int i=10; i<20; ++i)
mystack.push(1, i);
for(int i=100; i<110; ++i)
mystack.push(2, i);
for(int i=0; i<3; ++i)
cout<<mystack.top(i)<<" ";
cout<<endl;
for(int i=0; i<3; ++i){
mystack.pop(i);
cout<<mystack.top(i)<<" ";
}
mystack.push(0, 111);
mystack.push(1, 222);
mystack.push(2, 333);
for(int i=0; i<3; ++i)
cout<<mystack.top(i)<<" ";
return 0;
}