You are on page 1of 6

#include<conio.h> #include<iostream.h> #include<stdio.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,k; int cv; infor1 x; infor2 y; infor3 z; 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)) 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 insert(list F,list t,infor1 x,infor2 y,infor3 z) { list p; p=new element; strcpy((*p).ht,x);(*p).cc=y;(*p).cntc=z;

(*p).next=(*t).next; (*t).next=p;display(F); } void insertlast(list &F,infor1 x,infor2 y,infor3 z) { list p,t; p=new element; strcpy((*p).ht,x); (*p).cc=y; (*p).cntc=z; (*p).next=NULL; // Neu danh sach chua co phan tu if (F==NULL) F=p; else { // Duyet den cuoi t=F; while ((*t).next!=NULL) t=(*t).next; // chen vao cuoi danh sach (*t).next=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; // tim before va after 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) { int n; printf("chuong trinh nhap danh sach dac theo thu tu nguoc\n"); F=NULL; do { printf("\n nhap ho ten:");fflush(stdin); gets(x); if(strcmp(x,"")) { printf("nhap chieu cao:"); scanf("%f",&y); z=y*100-105; insertfirst(F,x,y,z); }} while(strcmp(x,"")); } void create2(list &F,list &L) {

int n,i=0; printf("nhap chuogn tirnh theo thu tu thuan\n"); F=NULL;L=NULL; printf("so nguoi trong danh sach :"); scanf("%d",&n); while(i<n) { printf("nhap ho ten:");fflush(stdin); gets(x); printf("\n nhap chieu cao:"); scanf("%d",&y); z=y*100-105; insertlast(F,x,y,z); i++; } } void create3(list &F) { int n,i=0; printf("chuong tirnh nhap theo thu tu ho ten\n"); F=NULL; printf("so nguoi trong danh sach la:"); scanf("%d",&n); while(i<n) { printf("phan tu thu %d",i); printf(" nhap ho ten:");fflush(stdin); gets(x); printf("nhap chieu cao:"); scanf("%f",&y); z=y*100-105; insertsort(F,x,y,z);i++; } }

void deletefirst(list &F) { list p; if(F!=NULL) { p=F; F=(*p).next; delete p; }display(F); } void deleteelement(list &F,list k) { list before,after; after=F; while((after!=NULL) &&(after!=k))

{ before=after; after=(*after).next; } if(F==k) F=(*k).next; else (*before).next=(*k).next; delete k; display(F); } void nhapds(list &F) { int n,i=0; F=NULL; printf("\n nhap vao so nguoi co trong danh sach\n"); scanf("%d",&n); printf("\n Nhap DS lien ket theo thu tu \n"); while(i<n) { printf("\n phan tu nhap thu %d :",i); printf("\n ho ten:"); fflush(stdin); gets(x); printf("\n Nhap chieu cao:");scanf("%f",&y); z=100*y-105; insertfirst(F,x,y,z); i++; }} void tim_L( list F,list &L) { list p; p=F; while (p!=NULL) { L=p; p=(*p).next; } } void delete_last(list &F) { // tim_L(F,L); deleteelement(F,L); } void display_last(list F) { printf("\n %20s,%7.2f %7d",(*L).ht,(*L).cc,(*L).cntc); } void display_first(list F) { list p; if(F!=NULL) { p=F; printf("\n %20s,%7.2f %7d",(*p).ht,(*p).cc,(*p).cntc); } } void main() { F=NULL; L=NULL;

int cv,key; do { printf("\n-----------------MENU----------------------\n"); printf("1.nhap moi danh sach \n"); printf("2.liet ke danh sach\n"); printf("3.them mot phan tu co ho ten le them chieu cao 1.55 vao dau danh sach\n"); printf("4.them mot phan tu co ho ten chieu cao cntc cho truoc vao dau danh sach\n"); printf("5.them pt co ho ten chieu cao cntc vao cuoi danh sach\n"); printf("6.tim nguoi te le tim\n"); printf("7.tim theo ten\n"); printf("8.xoa nguoi dau tien\n"); printf("10.xoa nguoi co ten LE XOA\n"); printf("9.xoa nguoi nhap tu ban phim\n"); printf("11.hien thi nguoi cuoi danh sach\n"); printf("13.hien thi phan tu dau danh sach\n"); printf("12.xoa nguoi o cuoi danh sach\n"); printf("14.ket thuc\n"); printf("chon cong viec:"); scanf("%d",&cv); switch(cv) { case 1: printf("ban muon nhap danh sach theo kieu nao?/t"); printf("\n 1.nhap theo thu tu nguoc"); printf("\n 2.nhap theo thu tu thuan"); printf("\n 3.nhap theo thu tu ho ten"); printf("\n------>key="); scanf("%d",&key); switch(key) { case 1:create1(F);break; case 2:create2(F,L);break; case 3:create3(F);break; default: printf("ko co thao tac nay"); }break; case 2: display(F);break; case 3: insertfirst(F,"le them",1.55,50); break; case 4:printf("nhap ho te can them:");fflush(stdin); gets(x); printf("nhap chieu cao:"); scanf("%",&y); z=y*100-105; insertfirst(F,x,y,z); break; case 5:printf("nhap ho ten can them:");fflush(stdin); gets(x); printf("nhap chieu cao :"); scanf("%d",&y); z=(y*100) -105; insertlast(F,x,y,z); break; case 6: p=search(F,"le tim"); if(p!=NULL) printf("\n %20f %7.2f %7d",(*p).ht,(*p).cc,(*p).cntc); else printf("tim khong co");break; case 7: printf("nhap ho ten can tim:");fflush(stdin); gets(x); p=search(F,x);

if(p!=NULL) printf("\n %20f %7.2f %7d",(*p).ht,(*p).cc, (*p).cntc); else printf("tim khong co"); case 8:deletefirst(F); break; case 9:printf("nhap ho ten can xoa:");fflush(stdin); gets(x); k=search(F,x); if(k!=NULL) deleteelement(F,k); else printf("\n tim khong co"); break; case 10:k=search(F,"LE XOA"); if(k!=NULL) deleteelement(F,k); else printf("\n ko thuc hien duoc");break; case 11: printf("\n phan tu cuoi la :\t"); tim_L(F,L); display_last(F) ; break; case 12: tim_L(F,L); delete_last(F);break; case 13:display_first(F); break ; }defaut:cv=0; } while(cv!=13); getch(); }

You might also like