栈的基本操作,请指点哪里出现错误。
各位大神,请看一下我的顺序栈的建立与输出到底哪出现错误,百思不得其解。我用的是DEV-C。#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int StackSize;
}sqStack;
//创建栈
void InitialStack(sqStack *S)
{
int a;
S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base)
printf("创建失败!\n");
else
{
S->top = S->base;
S->StackSize = STACK_INIT_SIZE;
scanf("%d",&a);
while(a!=-1&&((S->top )- (S->base )< (S->StackSize)))
{
*(S->top)=a;
S->top++;
scanf("%d",&a);
}
}
}
void StackPrintf(sqStack *S){
SElemType *p;
p=S->base;
while(p!=S->top)
{
printf(" %d",*p);
p++;}
}
int main(int argc, char *argv[]) {
sqStack *S;
printf("\n创建一个栈:\n");
InitialStack(S);
//输出栈
StackPrintf(S);
return 0;
}