完美栈

正文

原来我大一写代码就这么追求完美了。。多么好看的界面啊

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include <iostream.h>
typedef int selemtype;
int ok=0;
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef struct {
selemtype *base;
selemtype *top;
int stacksize;
int length;
}SqStack;
void InitStack(SqStack *s)
{
if (ok==1)
count<<" 线性表以前已创建!"<<endl;
else
{
s->base = (selemtype *) malloc (STACK_INIT_SIZE *sizeof(selemtype));
if(!s->base) exit(4);
s->top=s->base;
s->stacksize = STACK_INIT_SIZE;
s->length=0;
ok=1;
cout<<" 空栈已经建立!"<<endl<<endl;
}
}
void Destroy(SqStack *s)
{
free(s->base);
s->base=s->top=NULL;
s->stacksize=0;
s->length=0;
cout<<" 空栈已经销毁!"<<endl;
ok=0;
}
void MakeEmpty(SqStack *s)
{
if (s->top!=s->base)
{
s->base = (selemtype *) realloc (s->base,STACK_INIT_SIZE *sizeof(selemtype));
s->top=s->base;
s->length=0;
s->stacksize = STACK_INIT_SIZE;
}
cout<<" 栈已经为空!"<<endl<<endl;
}
void IsEmpty(SqStack *s)
{
if (s->base==s->top)
cout<<" 栈为空!"<<endl;
else cout<<" 栈不是空的!"<<endl<<endl;
}
void Getlength(SqStack *s)
{
cout<<" 栈的元素个数为:"<<s->length<<endl;
}
void Gettop(SqStack *s)
{
int e=0;
if (s->base == s->top)
cout<<" 空栈!"<<endl;
else
cout<<" 栈顶元素为:"<<*(s->top-1)<<endl;
}
void Push(SqStack *s,int e)
{
if(s->top - s->base>=s->stacksize)
{
s->base = (selemtype *) realloc (s->base,STACKINCREMENT *sizeof(selemtype));
if(!s->base) exit (2);
s->top=s->base + s->stacksize;
s->stacksize +=STACKINCREMENT;
}
*s->top++ = e;
++(s->length);
cout<<" 插入完成,当前栈顶元素是 --- "<<e<<endl<<endl;
}
void Pop(SqStack *s)
{
int e;
if(s->top==s->base)
cout<<" 栈是空的!"<<endl;
else
{
e= * --s->top;
cout<<" 栈顶元素"<<e<<"已经删除!"<<endl<<endl;
--(s->length);
}
}
void Visit(SqStack *s)
{
int *v;
if(s->top==s->base)
cout<<" 栈是空的!"<<endl;
else
{
cout<<" 栈底: ";
for(v=s->base;v!=s->top;v=v+1)
cout<< *v<<"->";
cout<<" 栈顶!"<<endl<<endl;
}
}
void menu()
{
cout<<"=====================Menu====================="<<endl;
cout<<" * 1-创建一个空栈 | 2-判断栈是否空 *"<<endl<<endl;
cout<<" * 3-数据元素入栈 | 4-栈顶元素出栈 *"<<endl<<endl;
cout<<" * 5-读取栈顶元素 | 6-将当前栈置空 *"<<endl<<endl;
cout<<" * 7-计算元素个数 | 8-读取所有元素 *"<<endl<<endl;
cout<<" * 9-将当前栈销毁 | 0-立即退出程序 *"<<endl;
cout<<"=============================================="<<endl;
cout<<" 请输入菜单中的功能所对应的序号:";
}
int main()
{
int a,e;
SqStack s;
do {
menu();
cin>>a;
if( ok==0&&(a!=1&&a!=0))
printf(" 多项式还没有建立,请你先建立一个多项式!\n"); else
{
switch(a)
{
case 1:
InitStack(&s);
break;
case 2:
IsEmpty(&s);
break;
case 3:
printf(" 请输入你要插入的元素:");
scanf("%d",&e);
Push(&s,e);
break;
case 4:
Pop(&s);
break;
case 5:
Gettop(&s);
break;
case 6:
MakeEmpty(&s);
break;
case 7:
Getlength(&s);
break;
case 8:
Visit(&s);
break;
case 9:
Destroy(&s);
break;
case 0:
cout<<" 谢谢使用,再见!"<<endl; break;
default:
cout<<" 输入错误,请核对后重试!"<<endl; break;
}
}
}while (a!=0);
return 0;
}


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!