C语言中用链表求城市坐标,C语言作业急!!!!!!!!!!!!链表的应用将若干城市的信息存 爱问知识人...

#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;}。

全部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值