|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
父老乡亲们,走过路过帮小生看一眼bug吧,程序就是小甲鱼数据结构和算法课上的二进制转十进制,这里我把malloc的内存free了一下,可void DestroyStack(sqStack *s)的时候出错了,百思不得其解~~~
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
void initStack(sqStack *s)
{
s->base = (ElemType*)malloc(sizeof(ElemType)*STACK_INIT_SIZE);
if(!s->base)
exit(0);
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack *s, ElemType e)
{
if(s->top - s->base >= s->stackSize)
{
s->base = (ElemType*)realloc(s->base, (s->stackSize + STACKINCREMENT)*sizeof(ElemType));
if(!s->base) exit(0);
s->top = s->base + s->stackSize;
s->stackSize += STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
ElemType Pop(sqStack *s)
{
if(s->top == s->base) return NULL;
return *--(s->top);
}
void ClearStack(sqStack *s)
{
s->top =s->base;
}
void DestroyStack(sqStack *s)
{
int i,len;
len = s->stackSize;
for(i=0;i<len;i++)
{
free(s->base);
s->base++;
}
s->base = s->top = NULL;
s->stackSize = 0;
}
int StackLen(sqStack s)
{
return (s.top-s.base);
}
int main()
{
ElemType sum=0,a=1,i,len;
char e;
sqStack *s;
s = (sqStack*)malloc(sizeof(sqStack));
initStack(s);
scanf("%c",&e);
while(e!='#')
{
Push(s, (int)(e-48));
scanf("%c",&e);
}
getchar();
len = StackLen(*s);
printf("栈的当前容量是:%d\n",len);
for(i=0; i<len; i++)
{
a *= 2;
sum += Pop(s)*a/2;
}
printf("%d",sum);
DestroyStack(s);
free(s);
s=NULL;
return 0;
}
|
|