Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- //By FamiHug
- typedef struct dslk{
- int info;
- struct dslk *link;
- } dsl;
- int init_stack(dsl *,int);
- int empty(int);
- int full(int,int);
- int push(int,int,dsl *,int);
- int pop(dsl *);
- int t=0,k;
- int main()
- {
- int temp,thuong,n,i;
- dsl *p,*l,*m;
- printf("CHUONG TRINH SU DUNG DANH SACH LIEN KET DON CAI DAT STACK\n");
- printf("_____CONVERT MOT SO TU HE THAP PHAN SANG HE NHI PHAN_____\n");
- printf("_______________________Version 1.0 ______________________\n");
- printf("=========================================================\n");
- printf("Nhap vao mot so nguyen duong: ");
- scanf("%d",&n);
- if(n>0)
- {
- //Tinh so cac so du
- thuong=n;
- k=0;
- while(thuong!=0)
- {
- thuong=thuong/2;
- k++;
- }
- //init
- l=NULL;
- p=NULL;
- t=init_stack(p,t);
- //tao stack voi so ngan =k va push cac gia tri vao stack
- thuong=n;
- m=NULL;
- for(i=1;i<=k;i++)//Nhap nguoc tu cuoi ds
- {
- p=(dsl *)malloc(sizeof(dsl));
- temp=thuong%2;
- t=push(t,temp,p,k);
- thuong/=2;
- if(m==NULL)
- {
- p->link=NULL;
- m=p;
- }
- else
- {
- p->link=m;
- m=p;
- }
- }
- l=p;
- //pop xuoi tu dau ds
- printf("Doi sang he nhi phan la: ");
- for(i=1;i<=t;i++)
- {
- printf("%d",pop(p));
- p=p->link;
- }
- }
- else
- printf("So nhap vao khong hop le. Chuong trinh ket thuc");
- return 1;
- }
- int init_stack(dsl *p, int t)
- {
- t=0;
- p=NULL;
- return t;
- }
- int empty(int t)
- {
- if(t<=0) return 1;
- else return 0;
- }
- int full(int t,int k)
- {
- if(t>=k) return 1;
- else return 0;
- }
- int push(int t,int x,dsl *p,int k)
- {
- if(full(t,k)==0)
- {
- p->info=x;
- }
- else printf("Stack tran");
- return t+1;
- }
- int pop(dsl *p)
- {
- int x=p->info;
- if(empty(t)==1) //Neu rong
- printf("Stack rong");
- else
- {
- p=p->link;
- }
- return x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement