数据结构_栈实现数值转换
2013-05-30 11:06
67 查看
//明白了一些道理。。
#include
#include
#include
#include
#define STACK_INIT_SIZE 100
#define STACKCREMENT 10
#define N 8
typedef struct
{
int
*base;//栈底
int
*top;//栈顶
int
stacksize;//栈容量
}SqStack;
int InitStack(SqStack &S)
{
S.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)
{
exit(0);
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return
1;
}
int Push(SqStack &S,int e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(int*)realloc(S.base,(S.stacksize+STACKCREMENT)*sizeof(int));
if(!S.base)
{
exit(0);
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKCREMENT;
}
*S.top++=e;
return
1;
}
int StackEmpty(SqStack S)
{
if(S.top==S.base)
{
return 1;
}
return
0;
}
int Pop(SqStack &S,int &e)
{
if(S.top==S.base)
{
return 0;
}
e=*--S.top;
return
1;
}
int main()
{
int
n,e;//n为要转换的十进制数,e为临时用的int变量
SqStack
S;
InitStack(S);//构造一个空栈
scanf("%d",&n);
while(n)
{
Push(S,n%8);
n/=8;
}
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
printf("\n");
return
0;
}
#include
#include
#include
#include
#define STACK_INIT_SIZE 100
#define STACKCREMENT 10
#define N 8
typedef struct
{
int
*base;//栈底
int
*top;//栈顶
int
stacksize;//栈容量
}SqStack;
int InitStack(SqStack &S)
{
S.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)
{
exit(0);
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return
1;
}
int Push(SqStack &S,int e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(int*)realloc(S.base,(S.stacksize+STACKCREMENT)*sizeof(int));
if(!S.base)
{
exit(0);
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKCREMENT;
}
*S.top++=e;
return
1;
}
int StackEmpty(SqStack S)
{
if(S.top==S.base)
{
return 1;
}
return
0;
}
int Pop(SqStack &S,int &e)
{
if(S.top==S.base)
{
return 0;
}
e=*--S.top;
return
1;
}
int main()
{
int
n,e;//n为要转换的十进制数,e为临时用的int变量
SqStack
S;
InitStack(S);//构造一个空栈
scanf("%d",&n);
while(n)
{
Push(S,n%8);
n/=8;
}
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
printf("\n");
return
0;
}
相关文章推荐
- 数据结构Java实现06----中缀表达式转换为后缀表达式
- 用栈链实现数值转换
- 实现IP地址与数值之间的转换
- 利用栈的数据结构实现中缀表达式到后缀表达式的转换
- 利用stringstream 类实现字符串和数值间转换
- Excel日期/时间存储数值转换为具体日期/时间(java实现)
- 作业02: 实现三个数值的转换。
- 数据结构Java实现06----中缀表达式转换为后缀表达式
- JS函数实现数值转换为人民币大写
- Oracle Number型数值存储与转换的实现详解
- Oracle Number型数值存储与转换的实现详解
- 数据结构Java实现06----中缀表达式转换为后缀表达式
- 结合stack数据结构,实现不同进制转换的算法
- Python实现列表转换成字典数据结构的方法
- SQL实现数据结构纵向转换
- 数据结构Java实现06----中缀表达式转换为后缀表达式
- 如何正确实现PHP字符串转换为数值的需求
- Python实现列表转换成字典数据结构的方法
- 在.NET Framework中,实现各种进制的数值间的转换
- 数据结构_数值转换