单链表应用(检索、插入、删除、遍历)C语言实现

这是在爱问上对一位朋友问题的解答,代码编的有点仓促,格式没有调的太好。


<script type="text/javascript" src="http://ads.adbrite.com/mb/text_group.php?sid=170596&col=3&br=1"></script>

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

typedef 
struct  _tagCity
{
  
char m_Name[20];
  
int m_Number;
  
struct _tagCity * m_pNext;
}
City;

City 
* head;

City 
*  Search (  char   * name )
{
  City 
*= head;
  
while ( p->m_pNext!= NULL ) {
    
if(strcmp(p->m_pNext->m_Name, name) == 0){
       
return p;
    }

    p
=p->m_pNext;
  }

  
return NULL;
}


void  Init(City *  p)
{
 
char name[20];
 printf(
"Please input the city's name:");
 scanf(
"%s",name);
 
while( Search(name) != NULL ) {
   printf(
"The city has been in the list ");
   printf(
"Please reinput a name:");
   scanf(
"%s",name);
 }


 strcpy(p
->m_Name, name);
 printf(
"Please input the city's population:");
 scanf(
"%d",&p->m_Number);
 p
->m_pNext = head->m_pNext;
 
return;
}


void  Add ()
{
 City 
*newcity = NULL;
 newcity 
= ( City * ) malloc ( sizeof ( City ) );
 
if ( NULL == newcity ) {
    printf(
"There is not enough memory!! ");
    
return ;
 }

 Init ( newcity );
 head
->m_pNext = newcity;
 printf(
"The new city has been added. ");
 
return;
}


void  Del ()
{
 
char name[20];
 City 
* p=NULL, *q;

 printf(
"Please input the name of the city which you want to delete:");
 scanf(
"%s",name);

 printf(
"Now searching... ");
 p 
= Search ( name );
 
if ( NULL == p ) {
   printf ( 
"The city "%s" has not been in the link. " ,name);
   
return;
 }

 q
=p->m_pNext;
 p
->m_pNext = q->m_pNext;
 q
->m_pNext = NULL;
 free (q);
 printf(
"The city "%s" has been deleted.",name);
}


void  Sum ()
{
 City 
* p=NULL;
 
int i = 0;
 
long sum=0;
 p
=head->m_pNext;
 
while(p!=NULL){
  sum 
+= (long) p->m_Number;
  p
=p->m_pNext;
  
++i;
 }

 printf(
"There are total %d city(ies). ",i);
 printf(
"The total population is %ld ", sum);
 
return ;
}


void  Search_City()
{
  
char name[20];
  City 
*=NULL;
  printf(
"Please input the name you want to search:");
  scanf(
"%s",name);
  printf(
"Now searching... ");
  p 
= Search ( name );
  
if ( NULL == p ) {
    printf(
"The city has not been in the list. ");
    
return;
  }

  p 
= p->m_pNext;
  printf(
"The detail of the city "%s": ",p->m_Name);
  printf(
"Name: %s ", p->m_Name );
  printf(
"Population: %d ", p->m_Number);
  
return;
}


void  menu()
{
  printf(
"              MENU " );
  printf(
"1. The total population ");
  printf(
"2. Search a city ");
  printf(
"3. Add a new city ");
  printf(
"4. Delete a exist city ");
  printf(
"5. The menu ");
  printf(
"6. Exit ");
  printf(
" Pleas input a number of the thing you want.");
  printf(
" %");
}


int  main()
{
 
int i,f=1;
 
char input[10];

 head 
= ( City * ) malloc ( sizeof( City ) );
 
if ( NULL == head ){
     printf(
"Not enough memory!! ");
     
return 1;
 }

 head
->m_pNext = NULL;

 menu();
 
while( f != 0 ) {
     i 
= atoi ( fgets(input, 3, stdin) );
     
switch (i) {
       
case 1 : Sum(); break;
       
case 2 : Search_City(); break;
       
case 3 : Add(); break;
       
case 4 : Del(); break;
       
case 5 : menu(); break;
       
case 6 : f=0break;
       
default: f=1;
     }

     printf(
"%");
 }

 
return 0;
 }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值