#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define OK 1
#define ERROR -1
#define STACK_SIZE 100
typedef int Elemtype;
//顺序栈的结构描述
struct Sq_stack
{
Elemtype stcak[STACK_SIZE];
Elemtype top;
};
typedef struct Sq_stack Sqstack;
//初始化一个顺序栈
void InitSqstack(Sqstack &S)
{
S.top=-1;
printf("\n---success---\n");
}
//入栈
void push(Sqstack &S,Elemtype data)
{
if(S.top==STACK_SIZE-1)
{
printf("\n---FULL---\n");
exit(ERROR);
}
else
{
S.top++;
S.stcak[S.top]=data;
}
}
//出栈
Elemtype pop(Sqstack &S)
{
Elemtype temp;
if(S.top==-1)
{
printf("\n---Empty---\n");
}
else
{
temp=S.stcak[S.top];
S.top--;
return temp;
}
}
//栈的打印
void print_stack(Sqstack &S)
{
Elemtype temp;
printf("输出该堆栈:");
if(S.top==-1)
printf("---Empty---\n");
temp=S.top;
while(S.top!=-1)
{
printf(" [%d] ",S.stcak[S.top]);
S.top--;
}
S.top=temp;
}
int main()
{
Elemtype temp,temp1,temp2;
Sqstack S;
InitSqstack(S);
do
{
printf("\n***请输入你想执行的功能***\n");
printf("(1)进栈 (2)退栈 (3)EXIT\n");
scanf("%d",&temp);
switch(temp)
{
case 1:
{
printf("\n请输入你想进栈的数据:");
scanf("%d",&temp1);
push(S,temp1);
print_stack(S);
}break;
case 2:
{
temp2=pop(S);
printf("退栈的数据为:(%d)\n",temp2);
print_stack(S);
}break;
}
}
while(temp!=3);
printf("****感谢您的使用****");
printf("\n");
}
C语言实现顺序栈
最新推荐文章于 2022-10-21 21:15:54 发布