堆栈问题
请问这个程序哪里出问题了?程序代码:
[color=#0000FF]#include<stdio.h> #include<stdlib.h> #define N 5 struct stack { int data; struct stack *next; }; struct stack *head,*p; int nodenum=0; struct stack *creatstack() //创建一个节点 { struct stack *pr; if((pr=malloc(sizeof(struct stack)))==NULL) { printf("Can not creat!"); exit(0); } pr->next=NULL; return pr; } void pushstack(int d) //压栈 { if(nodenum==0) { p=head=creatstack(); nodenum++; } else { p->next=creatstack(); nodenum++; } p->data=d; } int popstack() //弹栈 { struct stack *pr; int d; p=head; while((p->next)!=NULL) { pr=p; p=p->next; } d=p->data; free(p); pr->next=NULL; return d; } int main() { int popdata[5]={0},i,d; for(i=0;i<5;i++) { scanf("%d",&d); pushstack(d); } for(i=0;i<5;i++) { popdata[i]=popstack(); printf("popdata[%d]=%d\n",nodenum--,popdata[i]); } return 0; }
[/color]