您的位置:首页 > 理论基础 > 数据结构算法

数据结构_栈实现数值转换

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: