Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 单链表应用(检索、插入、删除、遍历)C语言实现
【标  题】:单链表应用(检索、插入、删除、遍历)C语言实现
【关键字】:
【来  源】:http://blog.csdn.net/dragonxie1983/archive/2006/10/24/1348405.aspx

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

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


Your Ad Here
#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;
 }
 
用非递归法求弧度的SIN值(C语言实现)利用泰勒级数:【上一篇】
No3 GUI框架 - wxWidgets库的编译:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • CSS速成手册(2)
  • RenderingHints 类
  • Sybase常见问题集锦(一)(zt)
  • 对MFC8.1的白日梦
  • SCO UNIX作业调度
  • 最全面介绍linux服务器原理和相关方法
  • tomcat负载均衡一例
  • 如何在中文环境下的Red Hat Linux Enterprise AS 3.0的环境中安装MQ 5
  • Bug2108072导致Instance Crash
  • 二、 基 本 资 料 型 态
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.