首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请大家帮小弟我看看哪里出了有关问题

2012-09-27 
请大家帮我看看哪里出了问题这是一个栈的基本操作的程序,可是为什么运行不了?跳出一个这个程序已停止工作

请大家帮我看看哪里出了问题
这是一个栈的基本操作的程序,可是为什么运行不了?跳出一个这个程序已停止工作的窗口?
#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 100
typedef struct
{
int *base;
int *top;
}SqStack;
void InitStack(SqStack *s)
{
s->base=(int *)malloc(STACKSIZE *sizeof(int));
if(s->base)
{
printf("memory is not enough!");
return;
}
s->top=s->base;
}
void DestroyStack(SqStack *s)
{
free(s->base);
s->top=s->base=NULL;
}
int StackEmpty(SqStack s)
{
return s.top==s.base;
}
int GetTop(SqStack s,int *e)
{
if(StackEmpty(s))
{
printf("\nthe stack is empty!");
return -1;
}
*e=*(s.top-1);
return 1;
}
int GetLength(SqStack s)
{
return s.top-s.base;
}
void Push(SqStack *s,int e)
{
if(s->top-s->base>=STACKSIZE)
{
printf("stack is full");

}
*s->top++=e;
}
int Pop(SqStack *s,int *e)
{
if(StackEmpty(*s))
{
printf("\nthe stack is empty");
return -1;
}
*e=*--s->top;
return 1;
}
main()
{
SqStack s;int i,e,n=5;
InitStack(&s);
printf("\nInput %d numbers for STACK:",n);
for(i=1;i<=n;i++)
{
scanf("%d",&e);
Push(&s,e);
}
i=GetTop(s,&e);
printf("\n the top element is:%d",e);
printf("\n Pops,print all elements:\n");
n=GetLength(s);
for(i=1;i<=n;i++)
{
Pop(&s,&e);
printf("%4d",e);

}
DestroyStack(&s);
printf("\n");
}

[解决办法]

C/C++ code
s->base=(int *)malloc(STACKSIZE *sizeof(SqStack));//问题1
[解决办法]
还有一个概念你要搞清楚,你malloc出来的空间是在堆上面的,是向高地址发展的空间。
你要模拟栈,切不可以往低地址空间扩展,所以按道理在栈满的情况下,top应该在你malloc分配地址的首地址,这样的话,感觉你的代码是不是完全不对了?
[解决办法]
C/C++ code
void InitStack(SqStack *s){    s->base=(int *)malloc(STACKSIZE *sizeof(int));    if(s->base == NULL)    {        printf("memory is not enough!");        return;    }    s->top=s->base;}
[解决办法]
修改好的程序
C/C++ code
#include<stdio.h>#include<stdlib.h>#define STACKSIZE 100typedef struct{        int *base;    int *top;}SqStack;void InitStack(SqStack *s){    s->base=(int *)malloc(STACKSIZE *sizeof(int));    if(s->base==NULL)    {        printf("memory is not enough!");        return;    }    s->top=s->base;}void DestroyStack(SqStack *s){    free(s->base);    s->top=s->base=NULL;}int StackEmpty(SqStack s){    return s.top==s.base;}int GetTop(SqStack s,int *e){    if(StackEmpty(s))    {        printf("\nthe stack is empty!");        return -1;    }    *e=*(s.top-1);    return 1;}int GetLength(SqStack s){    return s.top-s.base;}void Push(SqStack *s,int e){    if(s->top-s->base>=STACKSIZE)    {        printf("stack is full");            }    *s->top++=e;}int Pop(SqStack *s,int *e){    if(StackEmpty(*s))    {        printf("\nthe stack is empty");        return -1;    }    *e=*--s->top;    return 1;}void main(){    SqStack s;int i,e,n=5;    InitStack(&s);    printf("\nInput %d numbers for STACK:\n",n);    for(i=1;i<=n;i++)    {        scanf("%d",&e);        Push(&s,e);    }    i=GetTop(s,&e);    printf("\n the top element is:%d",e);    printf("\n Pops,print all elements:\n");    n=GetLength(s);    for(i=1;i<=n;i++)    {        Pop(&s,&e);        printf("%4d",e);            }    DestroyStack(&s);    printf("\n");} 


[解决办法]

探讨

引用:

C/C++ code

s->base=(int *)malloc(STACKSIZE *sizeof(SqStack));//问题1

如题,为什么要把int改为sqstack呢?我现在只是申请一个栈里的整型元素(栈底)啊?

[解决办法]
探讨

引用:

引用:

C/C++ code

s->base=(int *)malloc(STACKSIZE *sizeof(SqStack));//问题1

如题,为什么要把int改为sqstack呢?我现在只是申请一个栈里的整型元素(栈底)啊?

不用改,你申请的是100个int元素大小的栈空间,所以这样是对的。不过编程上习惯于定义一个栈……

热点排行