马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 世道变了 于 2013-3-16 19:30 编辑
先上代码 :#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SPACE 10
#define STACKINCREMENT 10
typedef int ElemType;
typedef struct stack
{
ElemType* top;
ElemType* base;
int stackbace;
}sqstack;
void stackinit(sqstack* s)
{
s->base=(ElemType *)malloc(sizeof(ElemType)*STACK_INIT_SPACE );
if(!s->base)
{
printf("Memory allocation failure !!!!");
return;
}
s->top=s->base;
s->stackbace=STACK_INIT_SPACE;
}
void Push(sqstack *s,ElemType n)
{
if(((s->top)-(s->base))>=(s->stackbace))
{
s->base=(ElemType*)realloc(s->base,(s->stackbace+STACKINCREMENT)*sizeof(ElemType));
if(!s->base)
{
printf("Memory increment allocation failure !!!!");
return;
}
}
*(s->top)=n;
s->top++;
}
void Pop(sqstack *s)
{
if(s->top==s->base)
{
printf("\nThe stack is empty!!!");
}
s->top--;
printf("\n%d 出栈\n",*(s->top));
}
void print(sqstack* s)
{
ElemType *p;
p=s->top;
while(p!=s->base)
{
p--;
printf("%d ",*p);
}
}
int main()
{
sqstack s;
int n;
stackinit(&s);
n=20;
while(n)
{
Push(&s,6);
n--;
}
print(&s);
Pop(&s);
print(&s);
}
当main函数内的n>=21时堆栈内就会有乱码生成 小于时什么问题都没有,我个人觉得应该是push函数内的realloc导致的,该怎么解决呢????
|