C语言学习之用链表实现通讯录
本程序主要功能是对联系人信息进行,添加、删除、查找、插入、显示功能
这里面有我实现的链表操作的接口的详细实现过程,并进行过测试的哦!!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "addressBookList.h"
/* 显示链表所有信息*/
void chainlist_all(chainListType *head)
{
chainListType *phead;
DATATYPE_T data;
phead = head;
while(phead)
{
data = phead->data;
printf("name:%s,address:%s,telephone:%s\n",data.key,data.add,data.telephone);
phead = phead->next;
}
return;
}
/*添加联系人*/
chainListType *add_contact(chainListType *head)
{
DATATYPE_T contactInfo;
printf("please input contact information\n");
scanf("%s %s %s",contactInfo.key,contactInfo.add,contactInfo.telephone);
return chainlist_add_end(head,contactInfo);
}
/*按照关键字查找联系人*/
int find_contact(chainListType *head)
{
char key[15];
chainListType *node = NULL;
printf("please input find key\n");
scanf("%s",key);
node = chainlist_find(head,key);
if(node!=NULL)
{
printf("find info name:%s,address:%s,telephone:%s\n",node->data.key,node->data.add,node->data.telephone);
}
else
{
printf("the key can't find!!!\n");
}
return 0;
}
/*按照关键字删除联系人*/
chainListType *delete_contact(chainListType *head)
{
char key[15];
chainListType *phead = NULL;
printf("please input delete key\n");
scanf("%s",key);
phead = chainlist_delete(head,key);
if(phead == NULL)
{
printf("delete after the list is NULL!!!\n");
return NULL;
}
return phead;
}
/*插入联系人信息*/
chainListType *insert_contact(chainListType *head)
{
char key[15];
DATATYPE_T insertData;
chainListType *phead = NULL;
printf("please input insert key\n");
scanf("%s",key);
printf("please input insert contact information\n");
scanf("%s %s %s",insertData.key,insertData.add,insertData.telephone);
phead = chainlist_insert(head,key,insertData);
return phead;
}
/*显示所有联系人信息*/
int show_contact(chainListType *head)
{
if(head==NULL)
{
printf("the list is NULL\n");
return -1;
}
chainlist_all(head);
return 0;
}
int menu()
{
printf("********************\n");
printf("1.add a contact\n");
printf("2.find a contact\n");
printf("3.delete a contact\n");
printf("4.insert a contact\n");
printf("5.show a contact\n");
printf("0.quit ");
printf("\n");
printf("********************\n");
}
int main()
{
int opt = 0;
chainListType *head=NULL;
do
{
printf("\n");
printf("please select option!\n");
menu();
scanf("%d",&opt);
printf("you select for %d\n",opt);
switch(opt)
{
case 1:
head = add_contact(head);
break;
case 2:
find_contact(head);
break;
case 3:
head = delete_contact(head);
break;
case 4:
head = insert_contact(head);
break;
case 5:
show_contact(head);
break;
case 0:
return 0;
default:
printf("unknow select\n");
break;
}
}while(opt!=0);
return 0;
}