基本操作在上一博文中已经叙述,下面就直接看关键代码:
//下面来编写一个实例来通过链表实现通信录
//1.定义通讯录结构
typedef struct
{
char key[15];//以姓名作为关键字
char address[20];
char teleNum[20];
char QQ[20];
}DATA;
#include "stdio.h"
#include "ChainList.h"
#include "ChainList.c"
void ChainListFindAll(ChainListType *head)
{
ChainListType *h;
DATA data;
h = head;
printf("链表所包含的内容如下\n");
while(h)
{
data = h->data;
printf("姓名:");
printf("%s\n",data.key);
printf("地址:");
printf("%s\n",data.address);
printf("电话:");
printf("%s\n",data.teleNum);
printf("QQ:");
printf("%s\n",data.QQ);
printf("<<<<<<<<<<<<<<<<\n");
h = h->next;
}
return;
}
ChainListType *input(ChainListType *head)
{
DATA data;
printf("请输入联系人信息\n");
printf("姓名:");
scanf("%s",data.key);
printf("地址:");
scanf("%s",data.address);
printf("电话:");
scanf("%s",data.teleNum);
printf("QQ:");
scanf("%s",data.QQ);
return ChainListAddFirst(head,data);
}
void find(ChainListType *head)
{
ChainListType *h;
DATA data;
char name[15];
printf("请输入查找姓名");
scanf("%s",name);
h = ChainListFind(head,name);
if(h)
{
data = h->data ;
printf("姓名:");
printf("%s\n",data.key);
printf("地址:");
printf("%s\n",data.address);
printf("电话:");
printf("%s\n",data.teleNum);
printf("QQ:");
printf("%s\n",data.QQ);
}
}
int main()
{
ChainListType *node,*head = NULL;
int select;
do
{
printf("\n______________\n");
printf("1.添加联系人\n");
printf("2.查找联系人\n");
//删除以后再说
printf("3.显示联系人\n");
printf("0,退出\n");
scanf("%d",&select);
printf("%d",select);
switch(select)
{
case 1:
printf("\n1.添加联系人\n");
head = input(head);
break;
case 2:
printf("\n2.查找联系人\n");
find(head);
break;
case 3:
printf("\n3.显示联系人");
ChainListFindAll(head);
break;
case 0:
return 0;
}
}while(select!=0);
}