栈在数制转换中的应用
2017-09-24 00:04
274 查看
#include <stdio.h> #define MAX_STACK_SIZE 100 #define ERROR 0 #define OK 1 typedef int ElemType; typedef int Status; typedef struct Stack_Node { ElemType data; struct Stack_Node *next; }Stack_Node; //栈的初始化 Stack_Node *Init_LInk_Stack(void) { Stack_Node *top; top = (Stack_Node *)malloc(sizeof(Stack_Node)); top->next = NULL; return (top); } //入栈 Status push(Stack_Node *top,ElemType e) { Stack_Node *p; p = (Stack_Node *)malloc(sizeof(Stack_Node)); if(!p) return ERROR; p->data = e; p->next = top->next; top->next = p; //钩链 return OK; } //弹栈(元素出栈)将元素顶出栈 Status pop(Stack_Node *top,ElemType *e) { Stack_Node *p; if(top->next==NULL) return ERROR; p=top->next; e = p->data; printf("%ld",e); //输出栈顶 top->next = p->next; free(p); } //数制转换(将十进制转化为d(2或8)进制数 void conversion(int n,int d) { Stack_Node *S; int k,*e; S = Init_LInk_Stack(); while(n>0) { k = n%d; push(S,k); n = n/d; } while(S->next!=0) //栈不空的时候出栈 { pop(S,e); } } int main() { int n; int d; printf("请输入十进制的数:"); scanf("%d",&n); printf("请输入所要转化成的进制数:"); scanf("%d",&d); conversion(n,d); return 0; }
相关文章推荐
- 栈的应用举例:数制转换,表达式求值
- 数据结构 中数制转换(栈的应用)
- 栈在数制转换上的应用
- 栈的应用——数制转换
- PHP数据结构之六 PHP栈的应用举例【数制转换和括号匹配算法】
- 【数据结构】栈的应用--数制转换(c++)
- 栈式应用举例-数制转换和后缀表达式求值
- 3.2栈的应用举例----数制转换、括号匹配及行编辑
- 算法:数制转换(栈的应用)-数据结构(6)
- 栈的应用数制转换
- 数据结构(8)--栈的应用之行编辑程序、括号匹配检验、数制转换、hanio塔问题
- 栈的应用:数制转换
- 数据结构题典020:栈的应用——数制转换(ANSI C)
- 数据结构-C语言 栈的简单应用——10进制以内的数制转换
- 7、数据结构笔记之七栈的应用之数制转换
- 栈的应用 (数制转换和括号匹配)
- 数制转换(栈的应用)
- 每天一个小程序(5)——栈的应用数制转换
- 第三章:顺序栈及其应用之三---数制转换
- 栈的应用1:数制转换、括号匹配检验和行编辑程序