欢迎来到天天文库
浏览记录
ID:47914363
大小:69.51 KB
页数:2页
时间:2019-10-25
《利用栈实现数制转换(10进制转换8进制)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//利用栈实现数制转换(10进制转换8进制)#include#include#defineERROR0#defineOK1#defineSTACK_INIT_SIZE100//存储空间初始分配量#defineSTACKINCREMENT10//存储空间分配增量typedefintSElemType;typedefstructstack{SElemType*top;SElemType*bottom;intstacksize;}SqStack;intInitSt
2、ack(SqStack*S){//构造一个空栈S->bottom=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S->bottom)returnERROR;//存储分配失败S->top=S->bottom;S->stacksize=STACK_INIT_SIZE;returnOK;}//InitStackintPush(SqStack*S,SElemTypee){//插入元素e为新的栈顶元素if(S->top-S->botto
3、m>=S->stacksize-1){S->bottom=(SElemType*)realloc(S->bottom,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));if(!S->bottom)returnERROR;//S->top=S->bottom+S->stacksize;}*S->top++=e;returnOK;}//PushintPop(SqStack*S,SElemType*e){//若栈不空,则删除S的栈顶元素,用e返回其值,并
4、返回OK;否则返回ERROR;if(S->top==S->bottom)returnERROR;*e=*--S->top;returnOK;}//PopintStackEmpty(SqStackS){if(S.top==S.bottom)return1;elsereturn0;}voidmain(){SqStackmyStack;intN,e;InitStack(&myStack);printf("请输入N:");scanf("%d",&N);while(N){Push(&myStack,N%8
5、);N=N/8;}while(!StackEmpty(myStack)){Pop(&myStack,&e);printf("%d",e);}printf("");}
此文档下载收益归作者所有