#include<stdio.h>
#include<malloc.h>
#define Elemtype int
#define FALSE 0
#define TRUE 1
typedef struct node //定义节点类型
{
Elemtype data;
struct node *next;
}LinkStackNode,*LinkStack;
int Push(LinkStack top,Elemtype x) //链栈元素插入
{
LinkStack temp;
temp=(LinkStackNode*)malloc(sizeof(LinkStackNode));
if(!temp) return FALSE;
temp->data=x;
temp->next=top->next;
top->next=temp;
return TRUE;
}
int Pop(LinkStack top,Elemtype x) 为什么要用x //链栈元素输出
{
LinkStack temp;
temp=top->next;
if(temp==NULL)
return FALSE;
top->next=temp->next;
*x=temp->data;
free(temp);
return TRUE;
}
int IsEmpty(LinkStack top)
{
LinkStack temp;
temp=top;
if(temp->next==NULL)
return TRUE;
else
return FALSE;
}
void Conversion(int N) //十进制转换成二进制
{
int x;
LinkStackNode S;
LinkStack top;
top=&S;
top->next=NULL;
while(N)
{
x=N%2;
Push(&S,x);
N=N/2;
}
while(!IsEmpty(&S))
{
Pop(&S,&x);
printf("%d",x);
}
}
void main()
{
int N;
printf(“please input a number:\n”);
scanf("%d",&N);
Conversion(N);
getch();
}