#include <stdio.h>
#include <conio.h>
#include <string.h>
typedef char infor1[20];
typedef float infor2;
typedef int infor3;
struct element
{ infor1 ht;
infor2 cc;
infor3 cntc;
element *next;
};
typedef element *List;
List F, L, p, t;
infor1 x;
infor2 y;
infor3 z;
int cv;
void Display(List F)
{ List p;
p=F;
while (p != NULL)
{ printf("\n %20s %7.2f %7d", (*p).ht , (*p).cc , (*p).cntc);
p=(*p).next;
}
}
List Search(List F, infor1 x)
{ List p;
p=F;
while ( (p!=NULL) && strcmp((*p).ht,x) !=0 )
p= (*p).next;
return p;
}
void InsertFirst(List &F, infor1 x, infor2 y, infor3 z)
{ List p;
p=new element;
strcpy((*p).ht,x); (*p).cc=y; (*p).cntc=z;
(*p).next=F;
F=p;
}
void InsertLast(List &F, List &L, infor1 x, infor2 y, infor3 z)
{ List p;
p=new element;
strcpy((*p).ht,x); (*p).cc=y; (*p).cntc=z;
(*p).next=NULL;
if (F==NULL) F=p;
else (*L).next=p;
L=p;
}
void InsertSort(List &F, infor1 x, infor2 y, infor3 z)
{ List p, before, after;
p=new element;
strcpy((*p).ht,x); (*p).cc=y; (*p).cntc=z;
after=F;
while ( (after!=NULL) && ( strcmp((*after).ht,x)<0 ) )
{ before=after;
after=(*after).next;
};
(*p).next=after;
if (F==after) F=p;
else (*before).next=p;
}
void Create1(List &F)
{ printf("\n Chuong trinh nhap moi danh sach theo thu tu nguoc");
F=NULL;
do
{ printf("\n nhap ho ten:"); fflush(stdin); gets(x);
if ( strlen(x)>0 )
{ printf("\n Nhap chieu cao:");
scanf("%f", &y);
z = (int)y*100-105;
InsertFirst(F,x,y,z);
}
} while ( strlen(x)>0 );
}
void Create2(List &F, List &L)
{ printf("\n Chuong trinh nhap moi danh sach theo thu tu thuan");
F=NULL; L=NULL;
do
{ printf("\n nhap ho ten:"); fflush(stdin); gets(x);
if ( strlen(x)>0 )
{ printf("\n Nhap chieu cao:");
scanf("%f", &y);
z = (int)(y*100-105);
InsertLast(F, L, x, y, z);
}
} while ( strlen(x)>0) ;
}
void Create3(List &F)
{ printf("\n Chuong trinh nhap moi danh sach theo thu tu ten");
F=NULL;
do
{ printf("\n nhap ho ten:"); fflush(stdin); gets(x);
if ( strlen(x)>0 )
{ printf("\n Nhap chieu cao:");
scanf("%f", &y);
z = (int)(y*100-105);
InsertSort(F,x,y,z);
}
} while ( strlen(x)>0 );
}
void DeleteFirst(List &F)
{ List p;
if (F!=NULL)
{ p=F;
F=(*p).next;
delete p;
}
}
void DeleteElement(List &F, List t)
{ List before, after;
after=F;
while ( ( after!=NULL) && (after!=t) )
{ before = after;
after=(*after).next;
}
if (after!=NULL)
{ if (F==t) F=(*t).next;
else (*before).next=(*t).next;
delete t;
}
}
main()
{ F=NULL; L=NULL;
do
{ printf("\n 1. Nhap moi danh sach sinh vien theo thu tu nguoc");
printf("\n 2. Liet ke danh sach");
printf("\n 3. Them 1 nguoi ten Le Them, cao 1.55 vao dau danh sach");
printf("\n 4. Them 1 nguoi vao dau danh sach");
printf("\n 5. Tim nguoi ten Le Tim");
printf("\n 6. Tim theo ten");
printf("\n 7. Xoa nguoi dau tien");
printf("\n 8. Xoa theo ho ten");
printf("\n 0. Ket thuc");
printf("\n An STT cong viec can thuc hien:");
scanf("%d", &cv);
switch (cv)
{ case 1: Create1(F);
break;
case 2: Display(F);
break;
case 3: z=1.55*100-105;
InsertFirst(F,"Le Them",1.55,z);
break;
case 4: printf("\n Nhap ho ten can them:");
fflush(stdin); gets(x);
printf("\n Nhap chieu cao:"); scanf("%f", &y);
z=y*100-105;
InsertFirst(F,x,y,z);
break;
case 5: t=Search(F,"Le Tim");
if (t!=NULL)
printf("\n %20s %7.2f %7d", (*t).ht , (*t).cc , (*t).cntc);
else printf("\n Tim khong co");
break;
case 6: printf("\n Nhap ho ten can tim:");
fflush(stdin); gets(x);
t=Search(F,x);
if (t!=NULL)
printf("\n %20s %7.2f %7d", (*t).ht , (*t).cc , (*t).cntc);
else printf("\n Tim khong co");
break;
case 7: DeleteFirst(F);
break;
case 8: printf("\n Nhap ho ten can xoa:");
fflush(stdin); gets(x);
t=Search(F,x);
if (t!=NULL)
DeleteElement(F,t);
else printf("\n Tim khong co");
break;
};
} while ( (cv>=1) && (cv<=8) );