链栈的入栈和出栈,求带神修改。
#include<stdio.h>#include<stdlib.h>
#define NULL 0
typedef struct link
{
int data;
struct link *next;
}link;
int Bulid(link *s)
{
s=NULL;
return 1;
}
link *push(link *s)
{
link *p;
printf("Enter the value to push until -1 over:");
for( ; ; ){
p=(link * )malloc(sizeof(link));
scanf("%d",&p->data);
if(p->data==-1)
{
break;
}
p->next=s;
s=p;
}
return s;
}
link *pop(link &s)
{
link *temp;
int num;
if(s==NULL)
{
printf("Stack is Empty!");
}
else
{
num=s->data;
temp=s;
s=s->next;
free(temp);
printf("出栈元素为%d",num);
return 1;
}
}
void main()
{
link *s;
int k;
char ch;
do
{
printf("\n");
printf("\n 1.初始化栈");
printf("\n 2.入栈");
printf("\n 3.出栈);
printf("\n 0.结束");
printf("\n==========================================");
printf("\n 请输入你的选择(1,2,3,0)");
scanf("%d",&k);
switch(k)
{
case 1:{
Bulid(link &s);
}break;
case 2:{
link *m;
m=push(link &s);
}break;
case 3:{
pop(link &m);
}break;
}
printf("\n-----------------------------------");
}while(k>=1&&k<4);
printf("\n 按回车键,返回...\n");
ch=getchar();
}