Code này mình chép trong quyển giáo trinh của trường , nhằm thực hiện yêu cầu:quản lý họ tên học sinh và điểm thi học kì của từng người(danh sách học sinh sắp theo thứ tự ABC)
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
typedef struct hoc_sinh {
char ho_ten[20];
float diem;
struct hocsinh *tiep;
} t_hoc_sinh;
t_hoc_sinh *head=NULL;
#define SIZESV (sizeof(t_hoc_sinh))
int main() {
t_hoc_sinh *sv,*p,*q;
char name[20];
float d;
char ch;
int n=0;
do {
printf("Nhap vao thong tin cua hoc sinh thu %d:\n",n+1);
fflush(stdin);
printf("Ho va ten:");
gets (name);
if(strcmp(name,"")!=0) {
sv=(t_hoc_sinh*)(malloc(SIZESV));
if(sv==NULL) {
printf("Khong du bo nho\n");
break;
} else {
n++;
strcpy(sv->ho_ten,name);
printf("Diem:");
scanf("%f",&d);
sv->diem=d;
sv->tiep=NULL;
if(head==NULL)
head=sv;
else {
p=head;
while(p!=NULL&&strcmp(p->ho_ten,sv->ho_ten)<0) {
q=p;
p=q->tiep;
}
if(p!=NULL&&strcmp(p->ho_ten,sv->ho_ten)==0) {
do {
fflush(stdin);
printf("Co hai hoc sinh trung ten(D/S)?");
} while(toupper(ch=getchar())!='D'&&toupper(ch)!='S');
if(toupper(ch)=='S') {
free (sv);
n--;
continue;
} else
while(p!=NULL&&strcmp(p->ho_ten,sv->ho_ten)==0) {
q=p;
p=p->tiep;
}
}
if(p==head) {
sv->tiep=head;
head=sv;
} else {
q->tiep=sv;
sv->tiep=p;
}
}
}
}
} while(strcmp(name,"")!=0);
p=head;
while(p!=NULL&&p->diem>=5.0) {
if(head==p)
head=q=p->tiep;
else {
q->tiep=p->tiep;
q=p->tiep;
}
free(p);
p=q;
while(p!=NULL&&p->diem<5.0) {
q=p;
p=p->tiep;
}
}
if(head=NULL)
printf("khong co hoc sinh thi lai");
else {
printf("Danh sach sinh vie phai thi lai\n\n");
n=0;
p=head;
while(p!=NULL) {
printf("%3d.%20s%6.1f\n",++n,p->ho_ten,p->diem);
p=p->tiep;
}
printf("\nCo tong %d hoc sinh phai thi lai",n);
}
p=head;
while(p!=NULL) {
q=p->tiep;
free(p);
p=q;
}
return 0;
}
Nhờ mọi người đọc giúp mình code trên với.Mình chỉ hiểu được đoạn:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
typedef struct hoc_sinh {
char ho_ten[20];
float diem;
struct hocsinh *tiep;
} t_hoc_sinh;
t_hoc_sinh *head=NULL;
#define SIZESV (sizeof(t_hoc_sinh))
int main() {
t_hoc_sinh *sv,*p,*q;
char name[20];
float d;
char ch;
int n=0;
do {
printf("Nhap vao thong tin cua hoc sinh thu %d:\n",n+1);
fflush(stdin);
printf("Ho va ten:");
gets (name);
if(strcmp(name,"")!=0) {
sv=(t_hoc_sinh*)(malloc(SIZESV));
if(sv==NULL) {
printf("Khong du bo nho\n");
break;
} else {
n++;
strcpy(sv->ho_ten,name);
printf("Diem:");
scanf("%f",&d);
sv->diem=d;
Phần còn lại mình chịu thua ,không hiểu gì hết cả.