vent
30-05-2008, 10:58
#include<conio.h>
#include<stdio.h>
typedef struct nhanvien
{
char ten;
char ngay;
float luong;
int gt;
} nv;
typedef struct tagnode
{
nv info;
struct tagnode *pNext;
} node;
typedef struct
{
node *pHead;
node *pTail;
}list;
node *getnode(nv a)
{
node *p;
p=new node;
p->info=a;(tại sao có thể gán p->ìno cho a được???, p là con trỏ, a là cấu trúc)
p->pNext=NULL;
return p;
}
void creatlist(list l)
{
l.pHead=NULL;
l.pTail=NULL;
}
void addhead(list l, node *new_ele)
{
if (l.pHead=NULL)
{
l.pHead=new_ele;
l.pTail=l.pHead;
}
else
{
new_ele->pNext=l.pHead;
l.pHead=new_ele;
}
}
void input (list &l)
{
int n;
node *p;
printf("muon nhap bao nhieu nhan vien:");
scanf("%d",&n);
creatlist(l);
for(int i=1;i<(n+1);i++)
{
nv a;
printf("ten nhan vien %d:",i);
scanf("%s",&a.ten);
printf("ngay sinh nhan vien %d:",i);
scanf("%s",&a.ngay);
printf("tien luong nhan vien %d:",i);
scanf("%f",&a.luong);
printf("gioi tinh nhan vien %d:",i);
scanf("%d",&a.gt);
p=getnode(a);
addhead(l,p);
}
}
void output(list l)
{
node *p;
int i=0;
for(p=l.pHead;p!=NULL;p=p->pNext)
{
i=i+1;
printf("hello");
printf("%s",p->info.ten);
printf("%s",p->info.ngay);
printf("%f",p->info.luong);
printf("%d",p->info.gt);
}
}
void main()
{
list l;
input(l);
printf("\n");
output(l);
printf("complete");
}
Em chạy tới output là no thoát ra không hiểu tại sao
#include<stdio.h>
typedef struct nhanvien
{
char ten;
char ngay;
float luong;
int gt;
} nv;
typedef struct tagnode
{
nv info;
struct tagnode *pNext;
} node;
typedef struct
{
node *pHead;
node *pTail;
}list;
node *getnode(nv a)
{
node *p;
p=new node;
p->info=a;(tại sao có thể gán p->ìno cho a được???, p là con trỏ, a là cấu trúc)
p->pNext=NULL;
return p;
}
void creatlist(list l)
{
l.pHead=NULL;
l.pTail=NULL;
}
void addhead(list l, node *new_ele)
{
if (l.pHead=NULL)
{
l.pHead=new_ele;
l.pTail=l.pHead;
}
else
{
new_ele->pNext=l.pHead;
l.pHead=new_ele;
}
}
void input (list &l)
{
int n;
node *p;
printf("muon nhap bao nhieu nhan vien:");
scanf("%d",&n);
creatlist(l);
for(int i=1;i<(n+1);i++)
{
nv a;
printf("ten nhan vien %d:",i);
scanf("%s",&a.ten);
printf("ngay sinh nhan vien %d:",i);
scanf("%s",&a.ngay);
printf("tien luong nhan vien %d:",i);
scanf("%f",&a.luong);
printf("gioi tinh nhan vien %d:",i);
scanf("%d",&a.gt);
p=getnode(a);
addhead(l,p);
}
}
void output(list l)
{
node *p;
int i=0;
for(p=l.pHead;p!=NULL;p=p->pNext)
{
i=i+1;
printf("hello");
printf("%s",p->info.ten);
printf("%s",p->info.ngay);
printf("%f",p->info.luong);
printf("%d",p->info.gt);
}
}
void main()
{
list l;
input(l);
printf("\n");
output(l);
printf("complete");
}
Em chạy tới output là no thoát ra không hiểu tại sao