#include"stdio.h"
typedef struct linkstack
{
int date;
linkstack *next;
} linkstack;
void initstack(linkstack **head)//初始化
{
(*head)=new linkstack;
(*head)->date=0;
(*head)->next=NULL;
}
int stackpush(linkstack *head,int e)//入栈
{
linkstack *s;
s=new linkstack;
s->date=e;
s->next=head->next;
head->next=s;
}
int stackpop(linkstack *head,int *s)//出栈
{
linkstack *p;
if(head->next=NULL)
return 0;
else
{
p=head->next;
*s=p->date;
head->next=head->next->next;
delete p;
}
}
int gettop(linkstack *head,int &date)//取栈顶元素
{
if(head->next=NULL)
return 0;
else
{
date=head->next->date;
}
}
int printfstack(linkstack *head)//输出栈
{
linkstack *s=head->next;
while(s)
{
printf("%d\t",s->date);
s=s->next;
}
}
int main()
{
int s;
linkstack *head;
initstack(&head);
for(int i=1;i<=5;i++)
{
printf("请输入进栈元素\n");
scanf("%d",&s);
stackpush(head,s);
}
stackpop(head,&s);
printf("出栈:%d\n",s);
gettop(head,s);
printf("栈顶\t%d\n",s);
printfstack(head);
}
25行, if(head->next=NULL)改为 if(head->next==NULL)
还有37行