#include <iostream.h>
#include <stdlib.h>

typedef struct _num{
int bit;
_num *next;
}list;
typedef list *node;

void init(node &l){
l=NULL;
}

node GetNode(int x){
node p;
p=(node)malloc(sizeof(list));
if(p==NULL){
cout<<"Khong du bo nho!";
return NULL;
}
p->bit=x;
p->next=NULL;
return p;
}

node AddFirst(node &l,node p){
if(l==NULL){
l=p;
}
else{
p->next=l;
l=p;
}
}

void output(node l){
node p=l;
while(p!=NULL){
cout<<p->bit;
p=p->next;
}
cout<<endl;
}

int main(int argc, char *argv[])
{
node l;
int x=-1,tmp;
while(x<0){
cout<<"X=";
cin>>x;
}
while(x>=2){
tmp=x%2;
AddFirst(l,GetNode(tmp));
x/=2;
}
AddFirst(l,GetNode(x));
cout<<"He co so 2 cua x = ";
output(l);
return 0;
}
Mình viết xong cho chạy thử thì nó văng lỗi khi xuất, mình nghĩ là do nó xuất phần tử NULL, mấy bạn sửa rồi giải thích dùm được không?