栈问题改错
#include <stdio.h>#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S);
void DestroyStack(SqStack *S);
void ClearStack(SqStack *S);
int StackEmpty(SqStack *S);
int StackLength(SqStack *S);
int GetTop(SqStack *S);
void Push(SqStack *S,int e);
int Pop(SqStack *S);
void print(SqStack *S);
main(){
SqStack S;
char c;
int i,k,choose,e,n,num,N;
k=1;
if(InitStack(&S)!=1) printf("存储分配失败!");
printf("输入栈长:");
scanf("%d",&n);
printf("元素入栈:\n ");
for(i=0;i<n;i++){
scanf("%d",&e);
Push(&S,e);
}
printf("\n\n\n\n");
printf("\n\t\t\t 选择要执行的操作 ");
printf("\n\t\t\t*******************************");
printf("\n\t\t\t 1. 取栈顶元素 ");
printf("\n\t\t\t 2.向栈插入一个元素 ");
printf("\n\t\t\t 3.删除一栈元素 ");
printf("\n\t\t\t 4.置栈空 ");
printf("\n\t\t\t 5.测栈空 ");
printf("\n\t\t\t 6.显示栈的长度 ");
printf("\n\t\t\t 7.显示栈中元素 ");
printf("\n\t\t\t*******************************\n");
while(k){
scanf("%d",&choose);
switch(choose){
case 1: if(GetTop(&S)==0) printf("该栈为空栈!\n");
else printf("栈顶为: %d\n",GetTop(&S));
break;
case 2: printf("输入一个元素: ");
scanf("%d",&e);
Push(&S,e);
print(&S);
break;
case 3: if(Pop(&S)==0) printf("顺序栈为空,出栈失败!\n");
else print(&S);
break;
case 4: ClearStack(&S); break;
case 5: if(StackEmpty(&S)==0) printf("栈已为空!\n");
else printf("该栈不为空!\n");
break;
case 6: printf("栈长为: %d\n",StackLength(&S)); break;
case 7: print(&S); break;
getchar();
printf("\nDo you want to continue?(Y/N)");
scanf("%c",&c);
if(c=='Y'||c=='y') k=1;
else k=0;
}
DestroyStack(&S);
}
int InitStack(SqStack *S){
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(S.base==NULL){
printf("顺序栈初始存储空间分配失败!\n");
exit(1);
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return 1;
}
int GetTop(SqStack *S){
if(S->top==S->base) return 0 ;
return *(S->top-1);
}
void Push(SqStack *S,int e){
int length;
length=S->top-S->base;
if(length>= S->stacksize){
S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(S->base==NULL) exit(1);
S->top=S->base+length;
S->stacksize+=STACKINCREMENT;
}
*(S->top++)=e;
}
int Pop(SqStack *S){
int e;
if((S->top - S->base)==0)
return 0;
else{
e=*(--S->top);
return e;
}
}
int StackLength(SqStack *S){
return S->top-S->base;
}
void print(SqStack *S){
int i,length;
length=S->top - S->base;
printf("此时顺序栈从栈底到栈顶依次为: ");
for(i=0;i<length;i++){
printf("%d ",S->base[i]);
}
}
void ClearStack(SqStack *S){
S->top=S->base;
}
int StackEmpty(SqStack *S){
if(S->top==S->base) return 0;
else return 1;
}
void DestroyStack(SqStack *S){
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=0;
}
这个程序有个错误不知道怎么改啊