#include
#include
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
struct Stack
{
char base;
char *top;
int stacksize;
};
struct Stack *InitStack(struct Stack *S) //创建空栈
{
S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));
if(!S->base) {printf("error!"); return 0;}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE ;
return S;
}
struct Stack *Push(struct Stack *S,char e) //向栈中插入元素
{
if(S->top-S->base==S->stacksize)
{
S->base=(char *)realloc(S->base,(S->stacksize+STACKINCREMENT * sizeof(char)));
if(!S->base) {printf("分配空间失败"); return 0;}
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top)=e;
S->top++;
return S;
}
void Pop(struct Stack *S,char e) //删除栈顶元素,并且返回其值
{
if(S->top==S->base) {printf("栈为空,无法删除栈顶元素"); return 0;}
e=(--S->top);
printf("删除的栈顶元素为:");
printf("%c\n",e);
}
int main()
{
struct Stack *S;
int e;
char i;
S=(struct Stack *)malloc(STACK_INIT_SIZE * sizeof(char));
if( !S->base) printf("error!");
InitStack(S);
printf("输入要插入的元素e:");
scanf("%c",&e);
Push(S,&e);
Pop(S,&i);
return 0;
}
以上是我写的c语言程序,运行的时候不管输入什么,输出的都是x,求哪位大神讲解下为什么,实在是百思不得其解