|
|
发表于 2018-6-18 19:46:53
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define INIT_STACK_SIZE 100
- #define INCREMENT 10
- typedef int ElemType;
- typedef struct
- {
- ElemType *base;
- ElemType *top;
- int stacksize;
- }sqStack;
- void InitStack(sqStack *s)
- {
- s->base = (ElemType*)malloc(INIT_STACK_SIZE*sizeof(ElemType));
- s->top = s->base;
- s->stacksize = INIT_STACK_SIZE;
- }
- void Push(sqStack *s, ElemType e)
- {
- if(s->top - s->base >= s->stacksize){
- s->base = (ElemType*)realloc(s->base, (s->stacksize + INCREMENT)*sizeof(ElemType));
- }
-
- *s->top++ = e;
- }
- void Pop(sqStack * s, ElemType *e)
- {
- if(s->top == s->base){
- return;
- }
- *e = *--s->top;
- }
- int StackLen(sqStack s)
- {
- return (s.top - s.base);
- }
- int main(void)
- {
- ElemType c;
- sqStack s;
- InitStack(&s);//stuctor
- printf("###### Input a Decimal Integer#######\n");
- printf(">>>");scanf("%d", &c);
- getchar();
- while(c)
- {
- Push(&s, c%2);
- c = c/2;
- }
-
- while(StackLen(s))
- {
- Pop(&s, &c);
- printf("%d ", c);
- }
- putchar('\n');
-
- return 0;
- }
复制代码 |
|