求助:这段顺序栈C代码中出栈函数的错误
程序代码:
include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 10 #define STACKINCREMENT 2 typedef int ElemType; typedef struct stack { ElemType *base; ElemType *top; int stacksize; }STACK; typedef struct stack *SqStack; void InitStack(SqStack index); void DestroyStack(SqStack index); int StackLength(SqStack index); int Push(SqStack index,ElemType e); int GetTop(SqStack index); int Pop(SqStack index,ElemType e); int main() { STACK s; SqStack point=&s; InitStack(point); printf("The length of Stack is %d!\n",StackLength(point)); Push(point,5); printf("Now the top element of Stack is %d!\n",GetTop(point)); printf("The length of Stack is %d!\n",StackLength(point)); Push(point,20); Push(point,30); printf("Now the top element of Stack is %d!\n",GetTop(point)); printf("The length of Stack is %d!\n",StackLength(point)); ElemType e; Pop(point,e); printf("The value of e is %d!\n",e); DestroyStack(point); return 0; } void InitStack(SqStack index) { index->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(index->base==NULL) { printf("Apply space is not successed!\n"); exit(1); } index->top=index->base; index->stacksize=STACK_INIT_SIZE; printf("Apply space is successed!\n"); } void DestroyStack(SqStack index) { index->base=index->top=NULL; index->stacksize=0; printf("Stack has destroy!\n"); } int StackLength(SqStack index) { return index->top-index->base; } int Push(SqStack index,ElemType e) { if(StackLength(index)>=index->stacksize) { ElemType *sq=(ElemType *)realloc(index->base,(StackLength(index)+STACKINCREMENT)*sizeof(ElemType)); index->base=sq; index->stacksize+=STACKINCREMENT; } *(index->top)=e; index->top++; return 1; } int GetTop(SqStack index) { if(index->top==index->base) { printf("Stack is Empty!There is no element!"); exit(1); } ElemType *flag=index->top-1; ElemType e=*flag; return e; } /*int Pop(SqStack index,ElemType e) 出栈操作,将出栈的元素保存到e中 { if(index->top==index->base) { printf("Stack is Empty!There is no Element!"); } // printf("求妹子!\n"); e=GetTop(index); index->top--; // printf("求妹子2!\n"); return 1; }*/ int Pop(SqStack index,ElemType e) // 出栈操作,将出栈的元素保存到e中 { if(index->top==index->base) { printf("Stack is Empty!There is no Element!"); } e=*(--index->top); return 1; }个人感觉从入栈函数,和求栈的长度函数中看程序中指针没问题,为什么出栈函数有错误?求高手解释下,顺便问问 销毁顺序栈DestroyStack()的写法,十分感谢!