#include
#include
typedef struct node
{
int data;
struct node *next;
}StackNode;
//置空栈
StackNode* Init_LinkStack(StackNode *top)
{
top=NULL;
return top;
}
//判空栈
int Empty_LinkStack(StackNode
*top)
{
if(top==NULL)
{
return 1;
}
else
{
return 0;
}
}
//入栈
StackNode* Push_LinkStack(StackNode *top,int x)
{
StackNode *s;
s=malloc(sizeof(StackNode));
s->data=x;
s->next=top;
top=s;
return top;
}
StackNode* Pop_LinkStack(StackNode *top,int *x)
{
StackNode *p;
if(top==NULL)
{
return NULL;
}
else
{
*x=top->data;
p=top;
top=top->next;
free(p);
return top;
}
}
int main()
{
StackNode top,*ls=&top,*p;
int select;
int y,z;
ls=Init_LinkStack(ls);
printf("\n(1)入栈");
printf("\n(2)出栈");
printf("\n(3)退出");
printf("\n请输入1-3\n");
scanf("%d",&select);
do
{
switch(select)
{
case 1:
printf("\n请输入入栈数据");
scanf("%d",&y);
ls=Push_LinkStack(ls,y);
printf("\n入栈结果");
p=ls;
while(p!=NULL)
{
printf("\n%d",p->data);
p=p->next;
}
break;
case 2:
ls=Pop_LinkStack(ls,&z);
printf("\n出栈结果");
p=ls;
while(p!=NULL)
{
printf("\n%d",p->data);
p=p->next;
}
break;
}
printf("\n(1)入栈");
printf("\n(2)出栈");
printf("\n(3)退出");
printf("\n请输入1-3\n");
scanf("%d",&select);
printf("\n");
}while(select!=3);
return 0;
}
阅读(1549) | 评论(0) | 转发(0) |