编写简易通讯录,要求使用“链表”这种存储结构,并附带相关操作。
需要功能:
1、使用链表存储联系人的相关信息,包括姓名、性别、电话号码、注释等,链表形式不限(推荐使用单链表,推荐事先准备一些数据)
2、有输出通讯录全部信息的功能
3、有清空通讯录的功能
4、有用户交互界面
5、有插入新联系人信息的功能
6、有查找联系人的功能,包括“按姓名查找”和“按电话号查找”两种
7、有删除联系人的功能,包括“按姓名删除”和“按电话号删除”两种
8、有按性别筛选联系人信息的功能
/*************************************************************************
@Author: wanghao
@Created Time : Tue 08 May 2018 10:12:00 PM PDT
@File Name: phone.c
@Description:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMESIZE 64
#define NUMSIZE 24
#define MARKSIZE 128
typedef struct node_t
{
char name[NAMESIZE];
char sex;
char number[NUMSIZE];
char remark[MARKSIZE];
struct node_t *pnext;
}Node;
Node nodedata[4] =
{
{"zixuan",'f', "10001", "haixing"},
{"yuge", 'm', "10002", "haikeyi"},
{"panxia", 'f', "10003", "yiban"},
{"haohao",'m', "10004", "haibucuo"}
};
/*Create empty list*/
Node *create_phone_empty_list(void);
/*Add phone list data by node*/
int add_phone_list_data(Node *listhead, Node *node);
/*Print phone list*/
int print_phone_list(Node *listhead);
/*Clear phone list*/
int clear_phone_list(Node *listhead);
/*Insert new connection data in head way*/
int insert_phone_list_data(Node *listhead);
/*Seek phone list data*/
int seek_phone_list_person(Node *listhead);
/*Delete phone list data*/
int delete_phone_list_person(Node *listhead);
/*Filter useful phone data with sex*/
int filter_phone_list_sex(Node *listhead, char sex);
int main(int argc, const char *argv[])
{
int i, datelength, quit, option;
Node *phead = NULL;
Node *newnode = NULL;
phead = create_phone_empty_list();
if(!phead)
{
printf("Create phone list fail!\n");
return -1;
}
datelength = sizeof(nodedata) / sizeof(Node);
for(i = 0; i< datelength; i++)
{
newnode = create_phone_empty_list();
if(!newnode)
{
printf("Create new node fail!\n");
return -2;
}
memcpy(newnode, &nodedata[i], sizeof(nodedata[i]));
add_phone_list_data(phead, newnode);
}
quit = 1;
while(quit)
{
system("clear");
printf("*****************************\n");
printf("Welcome to use the Address Book\n");
printf("*****************************\n");
printf("1:Output all contact information\n");
printf("2:Insert new Contact\n");
printf("3:Delete a contact\n");
printf("4:Find a contact\n");
printf("5:Empty contact information\n");
printf("6:Filter all male contacts\n");
printf("7:Filter all female contacts\n");
printf("0:Quit\n");
printf("*****************************\n");
printf("Please select the action to perform:");
scanf("%d",&option);
if(option < 0 || option > 7)
{
printf("Enter illegal data, please re-enter!\n");
}
switch (option)
{
case 0:
quit = 0;
break;
case 1:
print_phone_list(phead);
break;
case 2:
insert_phone_list_data(phead);
break;
case 3:
delete_phone_list_person(phead);
break;
case 4:
seek_phone_list_person(phead);
break;
case 5:
clear_phone_list(phead);
break;
case 6:
filter_phone_list_sex(phead, 'm');
break;
case 7:
filter_phone_list_sex(phead, 'f');
break;
default:
quit = 1;
}
printf("Please press ENTER to continue...!\n");
getchar();
while(getchar()!= '\n')
;
}
return 0;
}
Node *create_phone_empty_list(void)
{
Node *pnode = NULL;
pnode = (Node *)malloc(sizeof(Node));
if(!pnode)
{
printf("Malloc list node mem fail!\n");
return NULL;
}
pnode->pnext = NULL;
return pnode;
}
int add_phone_list_data(Node *listhead, Node *node)
{
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
node->pnext = listhead->pnext;
listhead->pnext = node;
return 0;
}
int insert_phone_list_data(Node *listhead)
{
Node *newnode;
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
newnode = create_phone_empty_list();
if(!newnode)
{
printf("Insert list data fail!\n");
return 1;
}
printf("Please input name:");
scanf("%s",newnode->name);
printf("Please input sex(m: man, f: feminine)");
getchar();
scanf("%c",&(newnode->sex));
printf("Please input number:");
scanf("%s",newnode->number);
printf("Please input remark information:");
scanf("%s",newnode->remark);
newnode->pnext = listhead->pnext;
listhead->pnext = newnode;
return 0;
}
int delete_phone_list_name(Node *listhead)
{
int find = 0;
Node *p = NULL;
Node *delete = NULL;
char name[NAMESIZE] = {0};
printf("please input name:");
scanf("%s",name);
p = listhead;
while(p->pnext)
{
if(strcmp(p->pnext->name, name) == 0)
{
printf("Find the record!\n");
printf("name: %s\n",p->pnext->name);
printf("sex: %c\n",p->pnext->sex);
printf("number: %s\n",p->pnext->number);
printf("remark: %s\n",p->pnext->remark);
delete = p->pnext;
p->pnext = delete->pnext;
free(delete);
delete = NULL;
printf("The record has been deleted!\n");
find = 1;
break;
}
p = p->pnext;
}
if(find == 0)
{
printf("Do not find %s record, please check it\n",name);
}
return 0;
}
int delete_phone_list_number(Node *listhead)
{
int find = 0;
Node *p = NULL;
Node *delete = NULL;
char number[NUMSIZE] = {0};
printf("please input number:");
scanf("%s",number);
p = listhead;
while(p->pnext)
{
if(strcmp(p->pnext->number, number) == 0)
{
printf("Find the record!\n");
printf("name: %s\n",p->pnext->name);
printf("sex: %c\n",p->pnext->sex);
printf("number: %s\n",p->pnext->number);
printf("remark: %s\n",p->pnext->remark);
delete = p->pnext;
p->pnext = delete->pnext;
free(delete);
delete = NULL;
printf("The record has been deleted!\n");
find = 1;
break;
}
p = p->pnext;
}
if(find == 0)
{
printf("Do not find %s record, please check it\n",number);
}
return 0;
}
int delete_phone_list_person(Node *listhead)
{
int option;
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
printf("*****************************\n");
printf("Please enter a way to find contacts!\n");
printf("1: In Name\n");
printf("2: In Number\n");
printf("0: Return\n");
printf("*****************************\n");
while(1)
{
printf("Please select:");
scanf("%d",&option);
if(option < 0 || option > 2)
{
printf("Enter illegal data please re-enter\n");
continue;
}
if(option == 0)
{
printf("About to return to the previous level, press ENTER to continue...\n");
return;
}
else
{
break;
}
}
if(option == 1)
{
delete_phone_list_name(listhead);
}
else
{
delete_phone_list_number(listhead);
}
}
int seek_phone_list_name(Node *listhead)
{
int find = 0;
Node *p = NULL;
char name[NAMESIZE] = {0};
printf("please input name:");
scanf("%s",name);
p = listhead;
while(p->pnext)
{
if(strcmp(p->pnext->name, name) == 0)
{
printf("Find the record!\n");
printf("name: %s\n",p->pnext->name);
printf("sex: %c\n",p->pnext->sex);
printf("number: %s\n",p->pnext->number);
printf("remark: %s\n",p->pnext->remark);
find = 1;
break;
}
p = p->pnext;
}
if(find == 0)
{
printf("Do not find %s record, please check it\n",name);
}
return 0;
}
int seek_phone_list_number(Node *listhead)
{
int find = 0;
Node *p =NULL;
char number[NUMSIZE] = {0};
printf("please input number:");
scanf("%s",number);
p = listhead;
while(p->pnext)
{
if(strcmp(p->pnext->number, number) == 0)
{
printf("Find the record!\n");
printf("name: %s\n",p->pnext->name);
printf("sex: %c\n",p->pnext->sex);
printf("number: %s\n",p->pnext->number);
printf("remark: %s\n",p->pnext->remark);
printf("The record has been deleted!\n");
find = 1;
break;
}
p = p->pnext;
}
if(find == 0)
{
printf("Do not find %s record, please check it\n",number);
}
return 0;
}
int seek_phone_list_person(Node *listhead)
{
int option;
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
printf("*****************************\n");
printf("Please enter a way to find contacts!\n");
printf("1: In Name\n");
printf("2: In Number\n");
printf("0: Return\n");
printf("*****************************\n");
while(1)
{
printf("Please select:");
scanf("%d",&option);
if(option < 0 || option > 2)
{
printf("Enter illegal data please re-enter\n");
continue;
}
if(option == 0)
{
printf("About to return to the previous level, press ENTER to continue...\n");
return;
}
else
{
break;
}
}
if(option == 1)
{
seek_phone_list_name(listhead);
}
else
{
seek_phone_list_number(listhead);
}
}
int clear_phone_list(Node *listhead)
{
Node *p = NULL;
Node *delete = NULL;
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
p = listhead;
while(p->pnext)
{
delete = p->pnext;
p->pnext = delete->pnext;
free(delete);
delete = NULL;
}
return 0;
}
int print_phone_list(Node *listhead)
{
Node *p = NULL;
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
p = listhead->pnext;
while(p)
{
printf("*****************************\n");
printf("name: %s\n",p->name);
printf("sex: %c\n",p->sex);
printf("number: %s\n",p->number);
printf("remark: %s\n",p->remark);
p = p->pnext;
}
printf("*****************************\n");
return 0;
}
int filter_phone_list_sex(Node *listhead, char sex)
{
Node *p = NULL;
if(!listhead)
{
printf("The list is not exist!\n");
return -1;
}
p = listhead->pnext;
while(p)
{
if(p->sex == sex)
{
printf("*****************************\n");
printf("name: %s\n",p->name);
printf("sex: %c\n",p->sex);
printf("number: %s\n",p->number);
printf("remark: %s\n",p->remark);
}
p = p->pnext;
}
printf("*****************************\n");
return 0;
}