歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言學習之用鏈表實現通訊錄

C語言學習之用鏈表實現通訊錄

日期:2017/3/1 9:33:29   编辑:Linux編程

本程序主要功能是對聯系人信息進行,添加、刪除、查找、插入、顯示功能

說明:調用的鏈表操作接口請參考:http://www.linuxidc.com/Linux/2015-02/113142.htm

這裡面有我實現的鏈表操作的接口的詳細實現過程,並進行過測試的哦!!!

#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;
}

C++ 隱式類類型轉化 Implicit Class-Type Conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

C語言變長數組之剖析 http://www.linuxidc.com/Linux/2013-07/86997.htm

C語言需要注意的問題 http://www.linuxidc.com/Linux/2013-05/84301.htm

C語言位域的使用及其注意點 http://www.linuxidc.com/Linux/2013-07/87027.htm

C語言中簡單的for循環和浮點型變量 http://www.linuxidc.com/Linux/2013-08/88514.htm

Copyright © Linux教程網 All Rights Reserved