-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path3-1-5.cpp
64 lines (63 loc) · 998 Bytes
/
3-1-5.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
#include<iostream>
using namespace std;
#define MAXSIZE 10
#define ELEMTYPE int
typedef struct{
ELEMTYPE data[MAXSIZE];
int top1;
int top2;
}SharedStack;
bool InitStack(SharedStack &s){
s.top1=0;
s.top2=MAXSIZE-1;
}
bool push1(SharedStack &s,ELEMTYPE x){
if(s.top1<=s.top2){
s.data[s.top1++]=x;
return true;
}
cout<<"error:push1"<<endl;
return false;
}
bool push2(SharedStack &s,ELEMTYPE x){
if(s.top2>=s.top1){
s.data[s.top2--]=x;
return true;
}
cout<<"error:push2"<<endl;
return false;
}
bool pop1(SharedStack &s,ELEMTYPE &x){
if(s.top1){
x=s.data[--s.top1];
return true;
}
return false;
}
bool pop2(SharedStack &s,ELEMTYPE &x){
if(s.top2<MAXSIZE-1){
x=s.data[++s.top2];
return true;
}
return false;
}
int main(){
SharedStack s;
InitStack(s);
int x;
cin>>x;
while(x!=9999){
push1(s,x);
cin>>x;
}
cin>>x;
while(x!=9999){
push2(s,x);
cin>>x;
}
while(pop1(s,x))cout<<x<<" ";
cout<<endl;
while(pop2(s,x))cout<<x<<" ";
cout<<endl;
return 0;
}