数据结构栈的问题 大家看看为什么出不了结果 最终出现内存不能written.....
用栈做一个进制转换 #include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define STACK_INIT_SIZE 100
#define ADD_SIZE 10
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int length;
}SqlStack;
Status InitStack(SqlStack *s) //初始化栈
{
s->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base)
exit(0);
s->top=s->base;
s->length=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqlStack *s) //取栈顶元素
{
if(s->top==s->base)
return ERROR;
return *(s->top-1);
}
void Push(SqlStack *s,int e) //入栈
{
if(s->top-s->base>=STACK_INIT_SIZE)
{
s->base=(SElemType*)realloc(s->base,(STACK_INIT_SIZE+ADD_SIZE)*sizeof(SElemType));
if(!s->base)
exit(0);
s->top=s->base+s->length;
s->length+=ADD_SIZE;
}
*s->top++=e;
}
Status Pop(SqlStack *s,SElemType e)//出栈
{
if(s->top==s->base)
return ERROR;
else
e=*--s->top;
return e;
}
Status EmptyStack(SqlStack *s)//判断栈空
{
if(s->top==s->base)
return 0;
else
return 1;
}
void ClearStack(SqlStack *s) //清空栈
{
s->top=s->base;
s->length=STACK_INIT_SIZE;
}
void DestroyStack(SqlStack *s) //销毁栈
{
s->top=NULL;
s->length=0;
free(s->base);
s->base=NULL;
}
void conversion() // 10进制转8进制
{
int num;
int e,e1;
SqlStack *L;
InitStack(L);
printf("\t请输入一个正整数:");
scanf("%d",&num);
while(num)
{
Push(L,num/8);
num%=8;
}
while(!EmptyStack(L))
{
e=Pop(L,e1);
printf("%d",e);
}
}
int main(void)
{
conversion();
return 0;
}