栈的基本操作啊
#include"stdio.h"#include"stdlib.h"
#include"malloc.h"
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
typedef int ElemType;
int InitStack(SqStack *S) //为栈S分配存储空间,并置S为空栈
{
int size = STACK_INIT_SIZE;
S->base=(int *)malloc(size*sizeof(ElemType));
if(!S->base) return 0;
S->top=S->base; //置栈S为空栈
S->stacksize=STACK_INIT_SIZE;
return 1;
}
int GetTop(SqStack *S,int e) //若栈不空,则用e返回S的栈顶元素
{
if(S->top==S->base) return 0;
e=*(S->top-1);
return 1;
}
int Push(SqStack *S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/
{
if(S->top-S->base>=S->stacksize) /*栈满,追加存储空间*/
{
int stackinvrement = STACKINCREMENT;
S->base=(ElemType *) realloc(S->base,
(S->stacksize+stackinvrement)*sizeof(ElemType));
if(!S->base) return 0; /*存储分配失败*/
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return 1;
}
int Pop(SqStack *S,int e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/
{
if(S->top==S->base) return 0;
e=*--S->top;
return 1;
}
void OutputStack(SqStack *S)
{int *q,i=0;
q=S->top-1;
for(i=0;i<S->top-S->base;i++)
{
printf("%d ",*q);q--;}
}
void main()
{
int a,b,c,d,e;
SqStack *h;
InitStack(h);
printf("你要输入几个元素:\n");
scanf("%d",&a);
printf("请输入你要的元素:\n");
for(b=0;b<a;b++)
{
scanf("%d",&c);Push(h,c);}
OutputStack(h);
GetTop(h,e);
Pop(h,e);
printf("返回的栈顶元素:%d\n",e);
我的程序有什么错呢就是输出不了自己想要的结果啊请各位大侠帮忙啊