#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node *next;
}LinkStack;
/*链栈的初始化操作*/
LinkStack *InitStack()
{
LinkStack *s;
s=(LinkStack *)malloc(sizeof(LinkStack));
s->next=NULL;
return s;
}
/*链栈的初始化进栈操作*/
int Push(LinkStack *s,ElemType e)
{
LinkStack *p;
p=(LinkStack *)malloc(sizeof(LinkStack));
p->data=e;
p->next=s->next;
s->next=p;
return OK;
}
/*链栈的初始化出栈操作*/
int Pop(LinkStack *s,ElemType *e)
{
LinkStack *p;
if(s->next==NULL)
return ERROR;
p=s->next;
*e=p->data;
s->next=p->next;
free(p);
return OK;
}
/*读取栈顶元素操作*/
int GetTop(LinkStack *s,ElemType *e)
{
LinkStack *p;
if(s->next==NULL)
{
return ERROR;
}else
p=s->next;
*e=p->data;
return OK;
}
/*链栈判空*/
int StackEmpty(LinkStack *s)
{
if(s->next==NULL)
return OK;
else
return ERROR;
}
/*菜单函数*/
void menue()
{
system("CLS");
printf("\n\n");
printf("||______________________________________________||\n");
printf("|| 菜单 ||\n");
printf("||----------------------------------------------||\n");
printf("|| (0) 初始化; ||\n");
printf("||----------------------------------------------||\n");
printf("|| (1) 进栈; ||\n");
printf("||----------------------------------------------||\n");
printf("|| (2) 出栈; ||\n");
printf("||----------------------------------------------||\n");
printf("|| (3) 取栈顶元素; ||\n");
printf("||----------------------------------------------||\n");
printf("|| (4) 判空; ||\n");
printf("||----------------------------------------------||\n");
printf("|| (5) 退出程序; ||\n");
printf("||______________________________________________||\n");
printf("请输入您的选择( )\b\b");
}
void main()
{
int x,y,select;
printf("\n\n\n\n\n\n\t\t\t欢迎使用顺序队列系统!\n");
system("pause");
while(1)
{
ElemType e;
LinkStack mystack;
menue();
scanf("%d",&select);
system("CLS");
switch(select)
{
case 0:
InitStack();
printf("该栈已初始化!\n");
system("pause");
break;
case 1:
printf("请输入一个数让其进栈:\n");
scanf("%d",&e);
y=Push(&mystack,e);
if(y==1)
printf("入栈的数为%d,入栈成功!\n",e);
else
printf("操作失败!\n");
system("pause");
break;
case 2:
y=Pop(&mystack,&e);
if(y==1)
printf("出栈的元素是:%d\n",e);
else
{
printf("此栈为空!\n");
}
system("pause");
break;
case 3:
x=GetTop(&mystack,&e);
if(x==1)
printf("取出的元素是:%d\n",e);
else
{
printf("此栈为空!\n");
}
system("pause");
break;
case 4:
y=StackEmpty(&mystack);
if(y==1)
printf("此栈为空!\n");
else
printf("此栈为不为空!\n");
system("pause");
break;
case 5:
exit(0);
default:
printf("输入错误,请重新输入!\n");
system("pause");
}
}
}
数据结构之链栈
最新推荐文章于 2021-12-08 01:23:00 发布