2.2.c

来自「自己写的数据结构课程程序」· C语言 代码 · 共 54 行

C
54
字号
#include<stdio.h>
#define NULL 0
struct Node
{int data;
 struct Node *next;
};
struct Stack
{struct Node *base;
 struct Node *top;
 int stacksize;
};
struct Stack S;
void InitStack()
{S.base=NULL;
 S.top=NULL;
 S.stacksize=0;
}
void Push(int x)
{struct Node *p;
 p=(struct Node*)malloc(sizeof(struct Node));
 p->data=x;
 p->next=S.top;
 S.top=p;
 S.stacksize++;
}
int StackEmpty()
{if(S.top==NULL)
   return 1;
 else
   return 0;
}
void Pop(int *p)
{*p=S.top->data;
 S.top=S.top->next;
 S.stacksize--;
}
void conversion()
{int N,e;
 InitStack();
 scanf("%d",&N);
 while(N)
   {Push(N%2);
    N=N/2;
   }
 while(!StackEmpty())
   {Pop(&e);
    printf("%d",e);
   }
}
void main(void)
{conversion();
 getch();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?