栈的应用----数制转换
2013-09-29 23:11
169 查看
应用栈先进后出的原则完成十进制转换成二进制
代码:
代码:
#include <stdio.h> #include <malloc.h> typedef struct node{ int data; struct node *next; }Stack; void StackInit(Stack *top) { top->next=NULL; } int Push(Stack *p,int x) { Stack *a; a=(Stack *)malloc(sizeof(Stack)); if(p==NULL) return 0; a->data=x; a->next=p->next; p->next=a; return 1; } int IsEmpty(Stack *top) //判断栈是否为空 { if(top->next==NULL) return 1; return 0; } int Pop(Stack *top,int *x) { Stack *p; if(IsEmpty(top)) return 0; p=top->next; *x=p->data; top->next=p->next; free(p); return 1; } int main() { int x; Stack *top; top=(Stack *)malloc(sizeof(Stack)); StackInit (top); //栈的初始化 printf("decimal:\n"); //输入十进制数 scanf("%d",&x); while(x) { Push(top,x%2); //进栈 x=x/2; } printf("binary:\n"); while(Pop(top,&x)) //出栈 { printf("%d",x); //输出二进制数 } printf("\n"); return 0; }
相关文章推荐
- 【数据结构】栈的应用--数制转换(c++)
- 栈的应用—数制转换
- 栈的应用之数制转换
- 第三章:顺序栈及其应用之三---数制转换
- 栈的应用举例----数制转换
- 数据结构--栈的基本操作及应用(数制转换)
- 栈式应用举例-数制转换和后缀表达式求值
- 5.栈的应用-数制转换和括号匹配
- 栈的应用举例——数制转换
- 栈操作和应用—数制转换
- 数据结构 中数制转换(栈的应用)
- 栈的基本应用-数制转换-数据结构
- 数制转换,将10进制转换为8进制 C语言 栈的应用
- 007.栈应用数制转换C实例
- 第六周--数据结构--队列的应用之数制转换(栈)
- 数制转换-栈的应用(C++实现)
- 栈的应用-数制转换
- 栈在数制转换中的应用
- 每天一个小程序(5)——栈的应用数制转换
- 栈的应用 (数制转换和括号匹配)