#include <stdio.h>
#include <malloc.h>
#define null 0
typedef struct stacknode
{
int data;
struct stacknode *next;
}stacklink;
typedef struct
{
int stacksize;
stacklink *top;
}linkstack;
void initlink(linkstack *s)
{
s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}
void pushlink(linkstack *s,int x)
{
stacklink *p;
p=(stacklink *)malloc(sizeof(stacklink));
p->data=x;
p->next=s->top->next;
s->top->next=p;
}
int poplink(linkstack *s)
{
int x;
stacklink *p;
if(s->top->next==null)
printf("the stack is empty\n");
else
{
x=s->top->next->data;
p=s->top->next;
s->top=s->top->next;
free(p);
}
return x;
}
void display(linkstack *s)
{
stacklink *p;
p=s->top->next;
printf("打印出该栈为:\n");
if (s->top=null) printf("the stacklink is empty!\n");
else {while(p)
{printf("-----%d",p->data);
p=p->next;}
}
}
void main()
{
int n,k,i,select,h,x1;
linkstack *p = (linkstack *)malloc(sizeof(linkstack));
initlink(p);
printf("请输入你所要创建的站长度:\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("请输入值:\n");
scanf("%d",&k);
pushlink(p,k);
}
printf("select 1:打印出该栈为\n");
printf("select 2:进栈操作\n");
printf("select 3:出栈操作\n");
printf("请选择:\n");
scanf("%d",&select);
switch(select)
{
case 1:{display(p);break;}
case 2:{printf("请输入值 :\n");
scanf("%d",&h);
pushlink(p,h);
display(p);
break;}
case 3:{x1=poplink(p);printf(" %d\n",x1);
display(p);
break;}
}
}
/*Thank you for your help!!*/