本程序主要功能是對聯繫人信息進行,添加、刪除、查找、插入、顯示功能
說明:調用的鏈表操作接口請參考:http://blog.csdn.net/qlx846852708/article/details/43482497
這裏面有我實現的鏈表操作的接口的詳細實現過程,並進行過測試的哦!!!
#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;
}