//Ho va ten: Tran Van Linh
//Msv:581597
//Lop:K58QLTT
#include<stdio.h>
#include<stdlib.h>
//Khai bao cau truc
struct node
{
int
infor;
struct
node *left;
struct
node *right;
} *T=NULL;
//khai bao ham
void push(int x);
int pop();
int empty();
//==chuong trinh chinh==
int main()
{
int
n,thuong;
printf("Nhap
so nguyen can chuyen sang dang nhi phan:");
do
{
scanf("%d",&n);
if(n<0)
printf("\nSo
nguyen nhap phai duong,nhap lai: ");
}while(n<0);
thuong=n;
while(thuong)
{
push(thuong%2);
thuong/=2;
}
printf("\nDang
nhi phan cua %d la:",n);
while(!empty())
printf("%d",pop());
return
0;
}
//==Dinh nghia ham==
void push(int x)
{
struct
node *N;
N=(struct
node*)malloc(sizeof(struct node));
N->infor=x;
N->left=NULL;
N->right=NULL;
//bo
sung vao dinh ngan xep
if(T==NULL)
T=N;
else
{
N->right=T;
T->left=N;
T=N;
}
}
//-------------
int pop()
{
int
tg;
struct
node *P;
if(T==NULL)
{
printf("Danh
sach rong.!");
return
0;
}
tg=T->infor;
P=T;
if(T->right==NULL) //Trường hợp ds chỉ có 1 nút
{
T=NULL;
}
else
{
T=T->right;
T->left=NULL;
}
free(P);
return
tg;
}
//-------------
int empty()
{
if(T==NULL)
return
1;
return
0;
}
//-------------
0 comments:
Post a Comment