zoukankan      html  css  js  c++  java
  • 菜鸟用链栈写的进制转换

     1 #include<stdio.h>  
    2 #include<stdlib.h>
    3
    4 struct Lnode/*以为当时做的题目没有告诉我是多少位的数字所以我选择了长度不受限的链栈*/
    5 {
    6 int num;
    7 struct Lnode *next;
    8 };
    9
    10 int Push(struct Lnode *top , int x)//压入数据
    11 {
    12 struct Lnode *p = (struct Lnode*)malloc(sizeof(struct Lnode));
    13 p->num = x ;
    14 p->next = top->next;//其实压入无非是链表的逆序输入,此为让新的节点指针域指向上一个节点
    15 top->next = p;
    16 return 0;
    17 }
    18
    19 int Pop(struct Lnode *top , int *x )//*x是为了得到弹出的数据
    20 {
    21 struct Lnode *q;
    22 q = top->next;
    23 if( top->next != NULL )//栈不为空的时候
    24 {
    25 *x = q->num;
    26 top->next = q->next;
    27 free(q);/*如果不释放q完全可以让p->next指向下一个,要注意的是这一步不可以跟上不颠倒,q毕竟只是指针,有指向的空间才有意义*/
    28 return 1;
    29 }
    30 else
    31 return 0;
    32 }
    33
    34 void convey(struct Lnode *top,int a ,int b )
    35 {
    36 int t,i,*x,c;
    37
    38 x=&c;
    39 while( a != 0 )
    40 {
    41 t = a % b;
    42 Push(top,t);
    43 a = a/b;
    44 }
    45
    46 i=1;
    47 while(i)
    48 {
    49 i=Pop(top,x);
    50 if(i)
    51 printf("%d",*x);
    52 }
    53 }
    54
    55 void main()
    56 {
    57 int a , b;
    58 struct Lnode *top;
    59 top = (struct Lnode *) malloc (sizeof(struct Lnode));
    60 top->next = NULL;
    61 scanf("%d%d", &a , &b);
    62 convey( top , a ,b );
    63 }
  • 相关阅读:
    多线程(一)
    Interface
    Abstract
    面向对象
    字符串比对(10分)
    快递费用计算(7分)
    练习7-8 方阵循环右移 (20分)
    习题4-11 兔子繁衍问题 (15分)
    一道题理解穷举/贪心/爬山/遗传/退火/蚁群算法
    华为暑期测试实习生面经(2020.06)
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2364058.html
Copyright © 2011-2022 走看看