关于栈的问题,没有错误,但是运行结果为什么多了那么多"4"
程序代码:
#include <stdio.h> #include <malloc.h> struct stack{ int elem; struct stack *top; }stacka; struct stack *init(struct stack *s){ s=(struct stack *)malloc(sizeof(struct stack)); s->top=NULL; return s; } void push(struct stack *s,int n){ struct stack *p; p=(struct stack *)malloc(sizeof(struct stack)); p->elem=n; p->top=s->top; s->top=p; } int pop(struct stack *s){ int e; struct stack *p; if(s->top!=NULL){ e=s->top->elem; p=s->top; s->top=p->top; free(p); return e; }else printf("栈空"); } void main(){ int i; struct stack *stack; stack=init(stack); for(i=0;i<3;i++) push(stack,i); for(i=0;i<7;i++) printf("%3d",pop(stack)); } 如果只想出现4 3 2 1 0 空栈 怎么办呢?