//MaSV:581597
//Lớp:K58QLTT
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
//khai bao cau truc
struct node
{
          int infor;
          struct node *left;
          struct node *right;
} *L=NULL,*R=NULL;
//khai bao ham
void Insert(int x);// chen mot nut
int Delete();//xoa mot nut
int empty();//kiem tra danh sach rong
//==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)
        {
              Insert(thuong%2);
              thuong/=2;
          }
         printf("\n%d chuyen sang dang nhi phan la:",n);
         while(!empty())
                 printf("%d",Delete());
        return 0;
}
//==Dinh nghia ham==
void Insert(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(L==NULL)
   {
           L=N;
           R=N;
    }
   else
  {
             N->right=L;
             L->left=N;
             L=N;
  }
}
//-------------
int Delete()
{
        int tg;
        struct node *P;
       if(L==NULL)
      {
            printf("Danh sach rong.!");
              return 0;
        }
      tg=L->infor;
      P=L;
      if(L->right==NULL)
     {
            L=NULL;
      }
     else
    {
            L=L->right;
            L->left=NULL;
    }
    free(P);
    return tg;
}
//-------------
int empty()
{
          if(L==NULL)
                 return 1;
           return 0;
}
//-------------






0 comments:
Post a Comment