• 10进制转8进制(栈操作)


    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    //#include <alloc.h>

    typedef int ElemType;
    /*
      创建链表式栈结构体
    */
    typedef struct Stack_Node
    {
       ElemType data;
       struct Stack_Node *next;
    }stack_node;
    /*
      初始化栈
    */
    stack_node* initStack()
    {
     struct Stack_Node *top;
     top=(struct Stack_Node*)malloc(sizeof(struct Stack_Node));
     top->next=NULL;
     return top;
    }
    /*
      进栈
    */
    stack_node* insertNode(stack_node *p,ElemType e)
    {
     struct Stack_Node *top;
     top=(struct Stack_Node*)malloc(sizeof(struct Stack_Node));
     if (!top)
     {
      printf("apply memory error! ");
      return p;
     }
     top->data=e;
     top->next=p->next;
     p->next=top;
     return p;
    }
    /*
      出栈
    */
    stack_node* popNode(stack_node *p)
    {
     struct Stack_Node *top;
     ElemType e;
     if (p->next==NULL)
     {
      printf("stack is empty! ");
      return p;
     }
        top=p->next;
     e=top->data;
     p->next=top->next;
     return p;
    }
    /*
      打印栈中的数据
    */
    void print(stack_node *p)
    {
     struct Stack_Node *top;
     top=p->next;
     while(top!=NULL)
     {
          printf("%d",top->data);
       top=top->next;
     }
    }
    void main()
    {
        struct Stack_Node *top;
     top=initStack();
     int n=1348,i;
        while(n!=0)
     {
           i=n % 8;
        insertNode(top,i);
        n=n / 8;
     }
     print(top);
        getchar();
    }

  • 相关阅读:
    我总结的面试题系列:kafka
    RabbitMQ大厂面试题
    [Algorithm] 并查集
    [LintCode] 编辑距离
    [LeetCode] Length of Longest Fibonacci Subsequence
    [LintCode] 交叉字符串
    [LeetCode] Permutation Sequence
    Permutation Sequence
    [LeetCode] Next Permutation
    [LeetCode] Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/batman425/p/3328507.html
Copyright © 2020-2023  润新知