主题:[讨论]大哥大姐们,帮个忙啊!急急
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack, *LinkStack;
/*构造一个空栈*/
Status initstack(LinkStack s)
{
int i;
s->base = (ElemType *) malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base) return OVERFLOW;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
/*入栈*/
Status push(LinkStack s,ElemType x)
{
if (s->top - s->base > s->stacksize)
{
s->base = (ElemType *)realloc(s->base,(s->stacksize + STACKINCREMENT)*sizeof(ElemType));
if (!s->base) exit(OVERFLOW);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*s->top++ = x;
return OK;
}
/*出栈*/
Status pop(LinkStack s,ElemType *x)
{
if (s->top == s->base) return ERROR;
else *x = *(--s->top);
return OK;
}
/*栈的遍历*/
void traverse(LinkStack s)
{ ElemType *p=s->top;
if(s->top==s->base)
printf("栈空");
else
while(s->top!=s->base)
{ s->top=s->top-1;
printf("%5d",*s->top);}
printf("\n");
s->top=p;
}
void main(){
SqStack *s;
ElemType e,*x;
int length,i,data;
int select;
do{
printf("1 建立一个空栈\n");
printf("2 元素入栈\n");
printf("3 元素出栈\n");
printf("0 退出,请输入选项(0-3)\n");
scanf("%d",&select);
switch(select){
case(0): break;
case(1): initstack(s);
printf("输入表长:");
scanf("%d",&length);
printf("\n输入数据:");
for(i=0;i<length;i++)
{ scanf("%d",&data);
push(s,data);}
traverse(s);
break;
case(2): printf("输入元素的值:");
scanf("%d",&e);
push(s,e);
traverse(s);
break;
case(3): printf("%d\n",pop(s,*x));
traverse(s);
break;
default:
printf("输入选项有错误,请重新输入!\n");
}
}while(select);
getch();
}
帮我看看这个程序哪错了,为什么弹出的栈顶元素总是1啊[em10]
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack, *LinkStack;
/*构造一个空栈*/
Status initstack(LinkStack s)
{
int i;
s->base = (ElemType *) malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base) return OVERFLOW;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
/*入栈*/
Status push(LinkStack s,ElemType x)
{
if (s->top - s->base > s->stacksize)
{
s->base = (ElemType *)realloc(s->base,(s->stacksize + STACKINCREMENT)*sizeof(ElemType));
if (!s->base) exit(OVERFLOW);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*s->top++ = x;
return OK;
}
/*出栈*/
Status pop(LinkStack s,ElemType *x)
{
if (s->top == s->base) return ERROR;
else *x = *(--s->top);
return OK;
}
/*栈的遍历*/
void traverse(LinkStack s)
{ ElemType *p=s->top;
if(s->top==s->base)
printf("栈空");
else
while(s->top!=s->base)
{ s->top=s->top-1;
printf("%5d",*s->top);}
printf("\n");
s->top=p;
}
void main(){
SqStack *s;
ElemType e,*x;
int length,i,data;
int select;
do{
printf("1 建立一个空栈\n");
printf("2 元素入栈\n");
printf("3 元素出栈\n");
printf("0 退出,请输入选项(0-3)\n");
scanf("%d",&select);
switch(select){
case(0): break;
case(1): initstack(s);
printf("输入表长:");
scanf("%d",&length);
printf("\n输入数据:");
for(i=0;i<length;i++)
{ scanf("%d",&data);
push(s,data);}
traverse(s);
break;
case(2): printf("输入元素的值:");
scanf("%d",&e);
push(s,e);
traverse(s);
break;
case(3): printf("%d\n",pop(s,*x));
traverse(s);
break;
default:
printf("输入选项有错误,请重新输入!\n");
}
}while(select);
getch();
}
帮我看看这个程序哪错了,为什么弹出的栈顶元素总是1啊[em10]