数据结构 - 出栈出现问题
程序代码:
# include <stdio.h> # include <stdlib.h> # define MAXSIZE 100 typedef struct { int * pbase; int * ptop; int stacksize; }Stack; void init(Stack * S) { int rom[MAXSIZE]; S->pbase = rom; if (NULL == S->pbase) exit(-1); S->ptop = S->pbase; S->stacksize = MAXSIZE; } void push(Stack * S) { int val; printf ("Enter the data:"); scanf ("%d", &val); *S->ptop = val; S->ptop++; } void pop(Stack * S) { int tmp; if (S->pbase == S->ptop) exit(-1); --(S->ptop); tmp = *S->ptop; printf ("%d\n", tmp); } int main (void) { Stack S; init(&S); push(&S); push(&S); pop(&S); pop(&S); return 0; }
龙芯8089d gentoo
编译之后,压栈没有问题,就是出栈的时候只有最后一个栈出来的数据是对的
比如输入 5 20 32
那么输出 32 随机数 随机数
代码哪里有错么?