码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构C语言—顺序栈案例—十进制转二进制

时间:2019-03-13 00:01:00      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:free   hide   ==   scan   esc   ide   copy   efi   cti   

技术图片
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "SeqStack.h"
 4 
 5 void conversion(int);
 6 SeqStack seq;
 7 int main()
 8 {
 9     int n;
10     scanf("%d",&n);
11     conversion(n);
12     return 0;
13 }
14 
15 void conversion(int N)
16 {
17     int * e = (int*)malloc(sizeof(int));
18     InitStack(&seq);
19     while(N)
20     {
21         PushStack(&seq,N%2);
22         N /= 2;
23     }
24     while(!IsEmpty(&seq))
25     {
26         Pop(&seq,e);
27         printf("%d",*e);
28     }
29     free(e);
30 }
main.c
技术图片
 1 #ifndef SEQSTACK_H_INCLUDED
 2 #define SEQSTACK_H_INCLUDED
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 /*****************************
 6 *project    :数据结构第三章案例
 7 *function   :定义顺序栈
 8 *Description:
 9 *Author        :中子
10 *****************************
11 *copyright:2019.3.12 by UZT
12 ****************************/
13 #define MAX_SIZE 100
14 #define OVERFLOW -1
15 
16 #define OK 1
17 #define FALSE -1
18 
19 typedef int Status;
20 typedef struct{
21     int * base;
22     int * top;
23     int stackSize;
24 }SeqStack;
25 
26 Status InitStack(SeqStack * seq);
27 
28 Status PushStack(SeqStack * seq,int element);
29 
30 Status Pop(SeqStack * seq,int * element);
31 
32 int IsEmpty(SeqStack * seq);
33 
34 #endif // SEQSTACK_H_INCLUDED
SeqStack.h
技术图片
 1 #include "SeqStack.h"
 2 Status InitStack(SeqStack * seq)
 3 {
 4     seq->base = (int*)malloc(MAX_SIZE);
 5     if(!seq->base) exit(OVERFLOW);
 6     seq->top = seq->base;
 7     seq->stackSize = MAX_SIZE;
 8     return OK;
 9 }
10 
11 Status PushStack(SeqStack * seq,int element)
12 {
13     //if(seq->top - seq->base == seq->stackSize) return FALSE;
14     *seq->top = element;
15     seq->top++;
16     return OK;
17 }
18 
19 Status Pop(SeqStack * seq,int * element)
20 {
21     if(seq->top - seq->base == 0) return FALSE;
22     seq->top--;
23     *element = *seq->top;
24     return OK;
25 }
26 
27 int IsEmpty(SeqStack * seq)
28 {
29     if(seq->base - seq->top == 0)
30         return 1;
31     else
32         return 0;
33 }
SeqStack.c

 

数据结构C语言—顺序栈案例—十进制转二进制

标签:free   hide   ==   scan   esc   ide   copy   efi   cti   

原文地址:https://www.cnblogs.com/cnlntr/p/10520368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!