#include<stdio.h>
#include<stdlib.h>
typedef struct stack
{
int data;
struct stack *next;
}LinkNode ,*LinkStack;
int initStack(LinkStack *S);//初始化
int push(LinkStack top);//进栈
int pop(LinkStack top);//出栈
int main(void)
{
int a;
int b;
LinkStack S = NULL;
while (1)
{
printf("\n");
printf("*******************\n");
printf("1.Init LinkStack\n");
printf("2.Push LinkStack\n");
printf("3.Pop LinkStack\n");
printf("4.EXIT\n");
printf("*******************\n");
scanf("%d",&a);
switch (a)
{
case 1:b = initStack(&S);
if (b == 1)
{
printf("InitStack Success!\n");
}
else
{
printf("Init Fail\n");
}
break;
case 2:b = push(S);
if (b == 0)
{
printf("Push Fail!\n");
}
if (b == 1)
{
printf("Push Success!\n");
}
break;
case 3:b=pop(S);
if (b == 0)
{
printf("Pop Fail\n");
}
if (b == 1)
{
printf("Pop Success\n");
}
break;
case 4:exit(0);
break;
default:printf("You input the figure is error\n");
printf("Please input the figure again:\n");
break;
}
}
}
int initStack(LinkStack *S)
{
(*S) = (LinkNode *)malloc(sizeof(LinkNode));
if (*S == NULL)
{
return 0;
}
(*S)->next = NULL;
return 1;
}
int push(LinkStack top)
{
LinkNode *p=NULL;
int a;
int flag=1;
printf("Please input the number that you want add(end by 0):\n");
while (flag)
{
scanf("%d",&a);
if (a == 0)
{
flag = 0;
continue;
}
p = (LinkNode *)malloc(sizeof(LinkNode));
if (p == NULL)
{
return 0;
}
p->data = a;
p->next = top->next;
top->next = p;
}
return 1;
}
int pop(LinkStack top)
{
LinkNode *p;
int a;
p = top->next;
if (p == NULL)
{
return 0;
}
while (p!=NULL)
{
a = p->data;
printf("%d\t",a);
top->next = p->next;
p = top->next;
}
printf("\n");
return 1;
}
链栈
最新推荐文章于 2022-08-08 12:12:55 发布