栈与队列应用:二进制转十进制 八进制 十六进制(栈)
1 //将二进制转化为十进制(利用后入先出的特点) 2 //二进制数 1101001 3 /* top 4 1 5 0 6 0 7 1 8 0 9 0 10 1 11 1 base */ 12 13 #include<stdio.h> 14 #include<stdlib.h> 15 #include<math.h> 16 17 #define STACK_INIT_SIZE 20 18 #define STACKINCREMENT 10 19 20 typedef char ElemType; //如果用int则相当于给计算机一个整形 21 22 typedef struct 23 { 24 ElemType *base; 25 ElemType *top; 26 int stackSize; 27 }sqStack; 28 29 void InitStack(sqStack *s) 30 { 31 s->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); 32 if(!s->base) 33 { 34 exit(0); 35 } 36 s->top = s->base; //初始化栈顶等于栈底 37 s->stackSize = STACK_INIT_SIZE; 38 } 39 40 void Push