#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct pp{
	char ht[25];
	char qq[30];
	int tuoi;
	struct pp *tiep;
}person;

void input(person *p, person *pdau, char *ht, char *qq){
	int t = 0;
	while(1){
		printf("\nHo ten (Bam enter de ket thuc nhap lieu): ");
		gets(ht);
		if(ht[0] == 0)
			break;
		if(pdau == NULL){
			pdau = (person*)malloc(sizeof(person));
			p = pdau;
		}
		else{
			p->tiep = (person*)malloc(sizeof(person));
			p = p->tiep;
		}
		strcpy(p->ht, ht);
		printf("\nQue quan: ");
		gets(p->qq);
		printf("\nTuoi: ");
		scanf("%d%*c", &t);
		p->tuoi = t;
		p->tiep = NULL;
	}
}

void inra(person *p, person *pdau){
	p = pdau;
	while (p != NULL){
		printf("\nHo ten %-25s Que %-30s Tuoi %d", (*p).ht, (*p).qq, (*p).tuoi);
		p = p->tiep;
	}	
}

void search(char *qq, person *p, person *pdau){
	while (1){
		printf("\nQue (Bam enter de ket thuc tim kiem): ");
		gets(qq);
		if (qq[0] == 0)
			break;
		p = pdau;
		while(p != NULL){
			if(strcmp(p->qq, qq)==0)
				printf("\nHo ten %-25s Que %-30s Tuoi %d\n", (*p).ht, (*p).qq, (*p).tuoi);
			p = p->tiep;
		}
	}
}

void loai(person *p, person *pdau){
	printf("\nLoai phan tu dau danh sach");
	if(pdau!=NULL){
		p = pdau;
		pdau = p->tiep;
		free(p);
	}
}

void bosung(person *p, person *pdau){
	int t = 0;
	printf("\nBo sung mot nguoi vao cuoi danh sach ");
	if(pdau==NULL){
		pdau = (person*)malloc(sizeof(person));
		p = pdau;
	}
	else{
		p = pdau;
		while(p->tiep!=NULL)
			p = p->tiep;
		p->tiep = (person*)malloc(sizeof(person));
		p = p->tiep;
	}

	printf("\nHo ten: ");
	gets(p->ht);
	printf("\nQue quan: ");
	gets(p->qq);
	printf("\nTuoi: ");
	scanf("%d%*c", &t);
	p->tuoi = t;
	p->tiep = NULL;
}

void chen(person *p, person *pdau, person *pl){
	int t = 0;
	printf("Chen them vao truoc nguoi thu hai");
	p = (person*)malloc(sizeof(person));
	printf("\nHo ten: ");
	gets(p->ht);
	printf("\nQue quan: ");
	gets(p->qq);
	printf("\nTuoi: ");
	scanf("%d%*c", &t);
	p->tuoi = t;

	pl= pdau->tiep;
	pdau ->tiep=p;
	p->tiep=pl;
}

void output(person *p, person *pdau){
	p = pdau;
	while(p!=NULL){
		printf("\nHo ten %-25s Que %-30s Tuoi %d", (*p).ht, (*p).qq, (*p).tuoi);
		p = p->tiep;
	}
	printf("\nHo ten %-25s Que %-30s Tuoi %d", (*p).ht, (*p).qq, (*p).tuoi);
}

void main(){
	int t;
	char ht[25], qq[30];
	person *pdau, *p, *pl;
	pdau = NULL;
	
	input(p, pdau, ht, qq);
	inra(p, pdau);
	search(qq, p, pdau);
	loai(p, pdau);
	bosung(p, pdau);
	chen(p, pdau, pl);
	output(p, pdau);

	free(p);
	free(pdau);
	free(pl);
}
