链式堆栈求助
#include<stdio.h>#include<stdlib.h>
struct stacktype
{
int data;
struct stacktype *link;
};
typedef struct stacktype stack;
int push(stack *top,int x)
{
stack *p;
p=(stack *)malloc(sizeof(stack));
if(p==NULL)
{
printf("Memory alloc failed\n");
exit(-1);
}
p->link=NULL;
if(top==NULL)
{
top=p;
top->data=x;
}
else
{
p->data=x;
p->link=top;
top=p;//这里我已经赋值了为什么top的地址值一直为0
}
return (1);
}
int pop(stack *top)
{
int x;
if(top==NULL)
{
printf("stack empty\n");
exit(0);
}
else
{
x=top->data;
top=top->link;
free(top);
}
return(x);
}
int main(void)
{
stack * top;
int i,n,x;
top=NULL;
printf("Input stack stize n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input x\n");
scanf("%d",&x);
push(top,x);
printf("%x\n",top);//打印top值为0
}
for(i=0;i<n;i++)
{
x=pop(top);
printf("%5d",x);
}
printf("\n");
}