Khi chạy nó chỉ in ra bệnh nhân đầu tiên. Em nghĩ là XuatMotBN(l.pHeap->info, i); sai nhưng không hiểu tại sao nó lại sai, Và sẽ sửa nó như thế nào. Mọi người cố gáng đọc tí code của em hơi dài. Xin cảm ơn!
#include <stdio.h>
struct BN
{
int maBN;
char hoten[30];
char ngaynhap[20];
float vienphi;
};
struct Node
{
BN info;
Node *Next;
};
struct List
{
Node *pHeap;
Node *pTail;
};
Node* TaoMotNode(BN x)
{
Node *p = new Node;
if(p == NULL)
{
printf("khong du bo nho");
return NULL;
}
p->info = x;
p->Next = NULL;
return p;
}
void NhapMotBN(BN &bn)
{
printf("Nhap ma so benh nhan: ");
scanf("%d", &bn.maBN);
printf("Nhap ho ten benh nhan: "); fflush(stdin);
gets(bn.hoten);
printf("Nhap ngay nhap vien: "); fflush(stdin);
gets(bn.ngaynhap);
printf("Nhap vien phi: ");
scanf("%f", &bn.vienphi);
}
void XuatMotBN(BN bn, int i)
{
printf("%-3d %-5d %-20s %-10s %10.2f\n", i, bn.maBN, bn.hoten, bn.ngaynhap, bn.vienphi);
}
void ThemDauDS(List &l, Node *x)
{
if(l.pHeap == NULL) //ds rong
l.pHeap = l.pTail = x;
/*
l.pHeap = x;
l.pTail = l.pHeap;
*/
else
{
x->Next = l.pHeap;
l.pHeap = x;
}
}
void TaoDS(List &l, int &n)
{
BN x;
Node *p = new Node;
for(int i=1; i<=n; i++)
{
NhapMotBN(x);
p= TaoMotNode(x);
ThemDauDS(l,p);
}
}
void Output(List l)
{
Node *p = l.pHeap;
int i=1;
printf("%-3s %-5s %-20s %-10s %10s\n", "STT", "MA BN", "HO TEN", "NGAY NHAP", "VIEN PHI");
while(l.pHeap != NULL)
{
XuatMotBN(l.pHeap->info, i); //có vấn đề
i++;
p = p->Next;
}
}
void main()
{
List l;
l.pHeap = l.pTail = NULL; //tao danh sach rong
int n;
printf("Nhap so luong benh nhan: ");
scanf("%d", &n);
TaoDS(l,n);
Output(l);
}