软讯网络 > 编程语言 > C/C++ > 单链表应用(检索、插入、删除、遍历)C语言实现
【标 题】:单链表应用(检索、插入、删除、遍历)C语言实现
【关键字】:
【来 源】:http://blog.csdn.net/dragonxie1983/archive/2006/10/24/1348405.aspx
单链表应用(检索、插入、删除、遍历)C语言实现

这是在爱问上对一位朋友问题的解答,代码编的有点仓促,格式没有调的太好。
#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 *p = 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 *p =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=0; break;
default: f=1;
}
printf("%");
}
return 0;
}
【相关文章】
没有相关文章