#include#include#includetypedefstruct_tagCity{charm_Name[20];intm_Number;struct_tagCity*m_pNext;}City;City*head;City*Search(char*name){City*p=head;while(p->m_pNext!=NULL){if(strcmp(p->m_pNext->m_Name,name)==0){returnp;}p=p->m_pNext;}returnNULL;}voidInit(City*p){charname[20];printf("Pleaseinputthecity'sname:");scanf("%s",name);while(Search(name)!=NULL){printf("Thecityhasbeeninthelist\n");printf("Pleasereinputaname:");scanf("%s",name);}strcpy(p->m_Name,name);printf("Pleaseinputthecity'spopulation:");scanf("%d",&p->m_Number);p->m_pNext=head->m_pNext;return;}voidAdd(){City*newcity=NULL;newcity=(City*)malloc(sizeof(City));if(NULL==newcity){printf("Thereisnotenoughmemory!!\n");return;}Init(newcity);head->m_pNext=newcity;printf("Thenewcityhasbeenadded。
\n");return;}voidDel(){charname[20];City*p=NULL,*q;printf("Pleaseinputthenameofthecitywhichyouwanttodelete:");scanf("%s",name);printf("Nowsearching。
。。\n");p=Search(name);if(NULL==p){printf("Thecity\"%s\"hasnotbeeninthelink。\n",name);return;}q=p->m_pNext;p->m_pNext=q->m_pNext;q->m_pNext=NULL;free(q);printf("Thecity\"%s\"hasbeendeleted。
",name);}voidSum(){City*p=NULL;inti=0;longsum=0;p=head->m_pNext;while(p!=NULL){sum+=(long)p->m_Number;p=p->m_pNext;++i;}printf("Therearetotal%dcity(ies)。
\n",i);printf("Thetotalpopulationis%ld\n",sum);return;}voidSearch_City(){charname[20];City*p=NULL;printf("Pleaseinputthenameyouwanttosearch:");scanf("%s",name);printf("Nowsearching。
。。\n");p=Search(name);if(NULL==p){printf("Thecityhasnotbeeninthelist。\n");return;}p=p->m_pNext;printf("Thedetailofthecity\"%s\":\n",p->m_Name);printf("Name:%s\n",p->m_Name);printf("Population:%d\n",p->m_Number);return;}voidmenu(){printf("MENU\n");printf("1。
Thetotalpopulation\n");printf("2。Searchacity\n");printf("3。Addanewcity\n");printf("4。Deleteaexistcity\n");printf("5。Themenu\n");printf("6。
Exit\n");printf("\n\nPleasinputanumberofthethingyouwant。
");printf("\n\n\n\n%");}intmain(){inti,f=1;charinput[10];head=(City*)malloc(sizeof(City));if(NULL==head){printf("Notenoughmemory!!\n");return1;}head->m_pNext=NULL;menu();while(f!=0){i=atoi(fgets(input,3,stdin));switch(i){case1:Sum();break;case2:Search_City();break;case3:Add();break;case4:Del();break;case5:menu();break;case6:f=0;break; }printf("%");}return0;}。
全部