tentuoi

#include<stdio.h> 

#include<conio.h> 

#include<alloc.h> 

#include<string.h> 

char tten[10]; 

typedef struct { 

char ten[10]; 

int tuoi; 

float dtb;

}kdulieu; 

typedef struct pt 

kdulieu dl; 

struct pt *ke; 

}kpt; 

void *trods; 

kdulieu hsi,hsi1; 

kpt *q; 

void *taodanhsach(void *trods); 

void xemdanhsach(void *trods); 

void sapxep(void *trods); 

void *bosungtruoc(void *trods, kpt *q, kdulieu hsi1); 

void *loaibo(void *trods, kpt *q);

void main() 

clrscr(); 

kpt *tam,*tam1,*tam2; 

float tg1,tg2; 

printf("

Tao danh sach :

"); 

trods=taodanhsach(trods); 

printf("

Xem danh sach :

"); 

xemdanhsach(trods); 

tam2=(kpt *)trods; 

//q=tam2->ke->ke; /* loai bo phan tu thu 3*/  

//q=tam2->ke;/*loai bo phan tu thu 2*/ 

//q=tam2; /*loai bo phan tu thu 1*/ 

/*tam2=(kpt *)trods; //loai bo 

while(tam2->ke!=NULL) //phan tu 

tam2=tam2->ke; //cuoi cung 

q=tam2; // 

*/ 

tam2=(kpt *)trods; //loai bo 

while(tam2->ke->ke!=NULL) //phan tu 

tam2=tam2->ke; //gan cuoi 

q=tam2; 

trods=loaibo(trods,q); 

printf("

Danh sach sau khi loai bo :

"); 

xemdanhsach(trods); 

/*strcpy(hsi1.ten,"Le Na"); 

hsi1.tuoi=20; 

hsi1.dtb=8.5; 

tam1=(kpt*)trods;*/ 

printf( "

Nhap thong tin sv can bo sung truoc

"); 

printf("

+ Nhap ten :");fflush(stdin);gets(tam1->dl.ten); 

printf("

+ Nhap tuoi:");scanf("%d",&tam1->dl.tuoi); 

printf("

+ Nhap dtb:");scanf("%f",&tg2);tam1->dl.dtb=tg2; 

strcpy(hsi1.ten,tam1->dl.ten); 

hsi1.tuoi=tam1->dl.tuoi; 

hsi1.dtb=tam1->dl.dtb; 

tam1=(kpt*)trods; 

//q=tam1->ke->ke; /* bo sung truoc phan tu thu 3 */ 

//q=tam1->ke;/*bo sung truoc phan tu thu 2*/ 

//q=tam1; /*bo sung truoc phan tu thu 1*/ 

/* 

tam1=(kpt *)trods; //bo sung truoc 

while(tam1->ke!=NULL) //phan tu 

tam1=tam1->ke; //cuoi cung 

q=tam1; // 

*/ 

tam1=(kpt *)trods; //bo sung truoc 

while(tam1->ke->ke!=NULL) //phan tu 

tam1=tam1->ke; //gan cuoi 

q=tam1; 

trods=bosungtruoc(trods,q,hsi1); 

printf("

Danh sach sau khi bo sung truoc

"); 

xemdanhsach(trods); 

getch();

void *taodanhsach(void *trods) 

trods=NULL; 

float d; 

do{ 

kpt *tam,*cuoi; 

printf("

+ Nhap ten(0 de thoat): "); 

fflush(stdin); 

gets(tten); 

if(strcmp(tten,"0")) 

tam=(kpt *)malloc(sizeof(kpt)); 

strcpy(tam->dl.ten,tten); 

printf("

+ Nhap tuoi: "); 

scanf("%d",&tam->dl.tuoi); 

printf("

+ Nhap diem trung binh:"); 

scanf("%f",&d); 

tam->dl.dtb=d; 

tam->ke=NULL; 

if(trods==NULL) (kpt *)trods=tam; 

else cuoi->ke=tam; 

cuoi=tam; 

}while (strcmp(tten,"0")); 

return trods; 

}

void xemdanhsach(void *trods) 

kpt *tam=(kpt*)trods; 

while(tam!=NULL) 

printf("

\t %-20s \t %d \t %5.2f

",tam->dl.ten,tam->dl.tuoi,tam->dl.dtb); 

tam=tam->ke; 

void sapxep(void *trods) 

kpt *tam1,*tam2; 

kdulieu t; 

tam1=(kpt*)trods; 

while(tam1->ke!=NULL){ 

tam2=tam1->ke; 

while(tam2!=NULL){ 

if((tam1->dl.tuoi)>(tam2->dl.tuoi)){ 

t=tam1->dl; 

tam1->dl=tam2->dl; 

tam2->dl=t; 

tam2=tam2->ke; 

tam1=tam1->ke; 

void *bosungtruoc(void *trods,kpt *q,kdulieu hsi1) 

kpt *tam,*tam1; 

tam=(kpt *)malloc(sizeof(kpt)); 

tam->dl=hsi1; 

tam->ke=q; 

if (q==trods) trods=tam; 

else 

tam1=(kpt *)trods; 

while(tam1->ke!=q) tam1=tam1->ke; 

tam1->ke=tam; 

return trods; 

void *loaibo(void *trods, kpt *q)

kpt *tam;if(tam->dl.tuoi==0){tam=q; 

if(q==trods) trods=q->ke; 

else{ 

tam=(kpt*)trods; 

while(tam->ke!=q) tam=tam->ke; 

tam->ke=q->ke; 

free(q);} 

return trods; 

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: