-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.1.cpp
74 lines (74 loc) · 1.46 KB
/
5.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
#include<iostream>
using namespace std;
#define datatype int
#define maxsize 100
typedef struct {
datatype data[maxsize];
int top;
}sqstack;
void init_sqstack(sqstack *sq) {
sq -> top = -1;
}
bool empty_sqstack(sqstack *sq) {
if(sq -> top == -1){
return true;
}
else{
return false;
}
}
bool push_sqstack(sqstack *sq, datatype x) {
if(sq -> top == maxsize -1){
return false;
}
else {
sq -> data[++ (sq -> top)] = x;
return true;
}
}
bool pop_sqstack(sqstack *sq, datatype *x){
if(empty_sqstack(sq)) {
return false;
}
else {
*x = sq -> data[sq -> top --];
return true;
}
}
int gettop_sqstack(sqstack *sq){
if(empty_sqstack(sq)){
cout<<"空"<<endl;
return NULL;
}
return sq -> data[sq -> top];
}
void print(sqstack *sq) {
int p = sq -> top;
while(p != -1) {
cout<<sq -> data[p--];
cout<<" ";
}
cout<<endl;
}
int main(void) {
sqstack stack;
sqstack *sq = &stack;
init_sqstack(sq);
int judge;
datatype x;
while(true){
cout<<"输入0,进行判断栈空,输入1进栈,输入2出栈,输入3输出,输入4取栈顶,输入其他结束。"<<endl;
cin>>judge;
switch(judge){
case 0:if(empty_sqstack(sq))cout<<"栈空"<<endl;break;
case 1:cout<<"请输入要插入的数据"<<endl;cin>>x;push_sqstack(sq, x);break;
case 2:pop_sqstack(sq, &x);cout<<"被删除的值是"; cout<<x<<endl;break;
case 3:cout<<"输出顺序栈"<<endl;print(sq);break;
case 4:cout<<"栈顶";cout<<gettop_sqstack(sq)<<endl;break;
default:return 0;
}
system("pause");
system("cls");
}
return 0;
}