Advertisement
Guest User

DecToBi_Stack_dsL_FamiHug

a guest
Nov 23rd, 2010
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //By FamiHug
  4.  
  5. typedef struct dslk{
  6.     int info;
  7.     struct dslk *link;
  8.     } dsl;
  9. int init_stack(dsl *,int);
  10. int empty(int);
  11. int full(int,int);
  12. int push(int,int,dsl *,int);
  13. int pop(dsl *);
  14. int t=0,k;
  15. int main()
  16. {
  17.    
  18.     int temp,thuong,n,i;
  19.     dsl *p,*l,*m;
  20.     printf("CHUONG TRINH SU DUNG DANH SACH LIEN KET DON CAI DAT STACK\n");
  21.     printf("_____CONVERT MOT SO TU HE THAP PHAN SANG HE NHI PHAN_____\n");
  22.     printf("_______________________Version 1.0 ______________________\n");
  23.     printf("=========================================================\n");
  24.     printf("Nhap vao mot so nguyen duong: ");
  25.     scanf("%d",&n);
  26.     if(n>0)
  27.     {
  28.         //Tinh so cac so du
  29.         thuong=n;
  30.         k=0;
  31.         while(thuong!=0)
  32.         {
  33.             thuong=thuong/2;
  34.             k++;
  35.         }
  36.        
  37.         //init
  38.         l=NULL;
  39.         p=NULL;
  40.         t=init_stack(p,t);
  41.         //tao stack voi so ngan =k va push cac gia tri vao stack
  42.         thuong=n;
  43.         m=NULL;
  44.         for(i=1;i<=k;i++)//Nhap nguoc tu cuoi ds
  45.         {          
  46.             p=(dsl *)malloc(sizeof(dsl));
  47.             temp=thuong%2;
  48.             t=push(t,temp,p,k);
  49.             thuong/=2;
  50.            
  51.             if(m==NULL)
  52.             {
  53.                 p->link=NULL;
  54.                 m=p;
  55.             }
  56.             else
  57.             {
  58.                 p->link=m;
  59.                 m=p;
  60.             }
  61.         }
  62.         l=p;
  63.        
  64.         //pop xuoi tu dau ds
  65.         printf("Doi sang he nhi phan la: ");
  66.         for(i=1;i<=t;i++)
  67.         {
  68.             printf("%d",pop(p));
  69.             p=p->link;
  70.         }
  71.    
  72.  
  73.  
  74.        
  75.        
  76.     }
  77.     else
  78.     printf("So nhap vao khong hop le. Chuong trinh ket thuc");
  79.    
  80.    
  81.     return 1;
  82. }
  83.  
  84. int init_stack(dsl *p, int t)
  85. {
  86.     t=0;
  87.     p=NULL;
  88.     return t;
  89. }
  90. int empty(int t)
  91. {
  92.     if(t<=0) return 1;
  93.     else return 0;
  94. }
  95.  
  96. int full(int t,int k)
  97. {
  98.     if(t>=k) return 1;
  99.     else return 0;
  100. }
  101.  
  102.  
  103. int push(int t,int x,dsl *p,int k)
  104. {
  105.     if(full(t,k)==0)
  106.     {
  107.         p->info=x;
  108.     }
  109.     else printf("Stack tran");
  110.     return t+1;
  111. }
  112.  
  113. int pop(dsl *p)
  114. {
  115.     int x=p->info;
  116.     if(empty(t)==1) //Neu rong
  117.     printf("Stack rong");
  118.     else
  119.     {
  120.         p=p->link;
  121.     }
  122.     return x;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement