顺序栈的基本操作
#include<stdio.h>
#include<stdlib.h>
#include<String.h>
#include<windows.h>
#define MaxSize 100
typedef struct {
int data[MaxSize];
int top;
}Sqstack;
Sqstack CreateStack(Sqstack S ){
S.top=-1;
printf("顺序栈已经初始化!\n");
return S;
}
Sqstack Push(Sqstack S, int e){
if(S.top==MaxSize-1)
printf("顺序栈已满!");
else{
S.data[++S.top] =e;
printf("%d已经入栈!",e);
}
return S;
}
Sqstack Pop(Sqstack S) {
int e;
if(S.top==-1)
printf("顺序栈为空!");
else{
e=S.data[S.top--];
printf("%d已经出栈!",e);
}
return S;
}
void ShowStack(Sqstack S) {
if(S.top==-1)
printf("栈中没有元素!");
while(S.top!=-1)
{
printf("%d ",S.data[S.top--]);
}
}
int main()
{
Sqstack Sa;
char str;
int e;
Sa=CreateStack(Sa);
printf("请选择顺序表操作:\nA:进栈\tB;出栈\tC;输出顺序栈\n");
while(str!='C')
{
printf("请选择操作:");
scanf("%s",&str);
switch(str){
case 'A':
printf("请输入进栈的值:");
scanf("%d",&e);
Sa=Push(Sa,e);break;
case 'B':Sa=Pop(Sa);break;
case 'C':ShowStack(Sa);break;
default:printf("输入不合理,请重新输入!");break;
}
}
}