Txl.h
#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_
typedef enum {CANCEL, ID, NAME, TEL, ADDRESS} OPTION;
typedef enum {TRUE, FALSE, ERROR} BOOL;
enum {QUIT, ADD, DISPLAY, DELETE, SEARCH, CHANGE, SORT};
typedef struct _Data
{
int id; //联系人id
char name[20]; //联系人姓名
long tel; //联系人手机号
char address[20]; //联系人地址
}Data;
typedef struct _Student
{
Data data; //数据域
struct _Student *next; //指针域,指向链表的下一个结点
}Student;
typedef struct _List
{
Student *head; //头结点
}List;
//创建通讯录
List *Creat_List();
//增加联系人
BOOL add_data(List *ls);
//查找联系人
BOOL search_data(List *ls);
//删除联系人
BOOL delete_data(List *ls);
//修改联系人
BOOL modify_data(List *ls);
//打印通讯录
void Display_List(List *ls);
//销毁通讯录
void Destroy_List(List *ls);
#endif //_LINK_LIST_H_
Txl.c
#include "Txl.h"
#include
#include
#include
//创建通讯录
List *Creat_List()
{
List *ls = (List *)malloc(sizeof(List)/sizeof(char));
if(NULL == ls) //创建链表失败,返回NULL
return NULL;
ls->head = (Student *)malloc(sizeof(Student)/sizeof(char));
if(NULL == ls->head)
{
free(ls); //创建头节点失败,先释放链表空间,再返回NULL
return NULL;
}
ls->head->next = NULL; //创建时第一个节点为空
return ls;
}
//输出单个联系人信息
void print_Student(Student *student)
{
if(student == NULL)
return;
printf("%-8d",student->data.id);
printf("%-16s",student->data.name);
printf("%-16ld",student->data.tel);
printf("%s\n",student->data.address);
}
//增加联系人
BOOL add_data(List *ls)
{
if(NULL == ls)
return ERROR;
Student *stu = (Student *)malloc(sizeof(Student)/sizeof(char)); //为新节点申请空间
if(NULL == stu)
return ERROR;
system("clear");
printf("输入新联系人ID:");
scanf("%d",&(stu->data.id));
printf("输入新联系人姓名:");
scanf("%s",stu->data.name);
printf("输入新联系人手机号:");
scanf("%ld",&(stu->data.tel));
printf("输入新联系人地址:");
scanf("%s",stu->data.address);
Student *tmp = ls->head;
while(tmp->next != NULL) //找到链表末尾
{
tmp = tmp->next;
}
tmp->next = stu; //将新联系人插入到链表末尾
stu->next = NULL;
return TRUE;
}
//显示所有联系人
BOOL display_data(List *ls)
{
if(NULL == ls) //链表不存在,返回ERROR
return ERROR;
if(ls->head->next == NULL)
{
return FALSE; //链表为空,返回FALSE,通讯录无联系人
}
Student *tmp = ls->head->next;
system("clear");
printf("---------------------------------------------------\n");
printf("id\t");
printf("name\t\t");
printf("tel\t\t");
printf("address\n");
while(tmp != NULL)
{
print_Student(tmp); //遍历联系人列表,同时打印已有联系人信息
tmp = tmp->next;
}
printf("---------------------------------------------------\n");
return TRUE;
}
//删除联系人
BOOL delete_data(List *ls)
{
if(NULL == ls)
return ERROR;
char delete[20]; //删除联系人姓名
BOOL flag = ERROR; //标记删除的状态,TRUE删除成功,FALSE删除失败,ERROR要删除的联系人不存在
system("clear");
printf("请输入删除的联系人姓名:");
scanf("%s", delete); //输入要删除的联系人姓名
Student *tmp = ls->head;
while(tmp->next != NULL) //遍历链表,直到匹配到要删的联系人姓名
{
if(strcmp(tmp->next->data.name, delete) == 0) //匹配成功
{
printf("---------------------------------------------------\n");
printf("id\t");
printf("name\t\t");
printf("tel\t\t");
printf("address\n");
print_Student(tmp->next); //打印匹配到的联系人的所有信息
printf("---------------------------------------------------\n");
char c = 0;
printf("确认删除该联系人yes(y)/no(n)\n");
while(c != 'y' && c!= 'n') //对删除操作进行确认,y确认和n取消,输入其他时要重新输入
{
scanf("\n%c", &c);
if(c == 'y')
{
Student *p = tmp->next; //删除联系人
tmp->next = p->next;
free(p); //释放删除联系人节点的空间
flag = TRUE; //flag标记删除成功
}
else if(c == 'n')
{
//如果之前已经删除过其他,不改变flag,依然标记删除成功
if(flag != TRUE)
{
flag = FALSE; //flag标记删除失败
}
}
else
{
printf("输入有误,请重新输入\n");
}
}
if(c == 'y')
{
//如果选择删除,tmp已经移动到了删除的人后一位,用continue跳过tmp = tmp->next这一步
continue;
}
}
tmp = tmp->next;
}
return flag; //返回flag
}
//修改联系人
BOOL modify_data(List *ls)
{
if(NULL == ls)
return ERROR;
char modify_name[20]; //要修改的联系人的姓名
int modify; //要修改的选项
BOOL flag = ERROR; //TRUE修改成功,FALSE修改失败,ERROR要修改的联系人不存在
system("clear");
printf("请输入要修改的联系人的姓名:");
scanf("%s", modify_name); //输入要修改联系人的姓名
Student *tmp = ls->head->next;
while(tmp != NULL) //遍历链表,直到匹配到要修改的联系人姓名
{
if(strcmp(tmp->data.name, modify_name) == 0) //匹配成功
{
//输出当前匹配到的联系人的所有信息
printf("---------------------------------------------------\n");
printf("id\t");
printf("name\t\t");
printf("tel\t\t");
printf("address\n");
print_Student(tmp);
printf("---------------------------------------------------\n");
printf("1.ID\n");
printf("2.姓名\n");
printf("3.手机号\n");
printf("4.家庭地址\n");
printf("0.放弃修改\n");
printf("请输入要修改的选项:");
scanf("%d", &modify); //输入要修改的选项
switch(modify)
{
case ID:
printf("将id修改为:\n");
scanf("%d", &(tmp->data.id));
flag = TRUE;
break;
case NAME:
printf("将姓名修改为:\n");
scanf("%s", tmp->data.name);
flag = TRUE;
break;
case TEL:
printf("将手机号修改为:\n");
scanf("%ld", &(tmp->data.tel));
flag = TRUE;
break;
case ADDRESS:
printf("将家庭地址修改为:\n");
scanf("%s", tmp->data.address);
flag = TRUE;
break;
case CANCEL:
system("clear");
if(flag != TRUE)
flag = FALSE; //取消修改时flag=FALSE,表示修改失败
break;
default:
printf("输入有误,请重新输入\n");
break;
}
}
tmp = tmp->next;
}
return flag;
}
//查找联系人
BOOL search_data(List *ls)
{
if(NULL == ls)
return ERROR;
char search[20] = {0}; //查找的联系人姓名
BOOL flag = FALSE; //标记是否找到,TRUE为找到,FALSE为没找到
system("clear");
printf("请输入查找的联系人姓名:");
scanf("%s", search);
Student *tmp = ls->head->next;
while(tmp != NULL)
{
if(strcmp(tmp->data.name, search) == 0) //匹配到要查找的联系人姓名
{
if(flag != TRUE) //找到第一个时打印输出格式
{
printf("---------------------------------------------------\n");
printf("id\t");
printf("name\t\t");
printf("tel\t\t");
printf("address\n");
flag = TRUE; //flag标记为已找到
}
print_Student(tmp); //打印找到的联系人信息
}
tmp = tmp->next; //tmp指向下一个节点
}
return flag;
}
//销毁通讯录
void Destroy_List(List *ls)
{
if(NULL == ls)
return ;
Student *tmp = ls->head;
while(tmp->next) //释放链表中各个节点的空间
{
Student *p = tmp->next;
tmp->next = p->next;
free(p);
}
free(ls->head);
free(ls);
}
menu.c
#include "Txl.h"
#include
#include
//菜单
void menu(List *ls)
{
int com = 0;
BOOL search_flag;
BOOL delete_flag;
BOOL modify_flag;
while(1)
{
printf("\t**************\n");
printf("\t*1.增加联系人*\n");
printf("\t*2.显示联系人*\n");
printf("\t*3.删除联系人*\n");
printf("\t*4.查找联系人*\n");
printf("\t*5.修改联系人*\n");
printf("\t*0.退出程序 *\n");
printf("\t**************\n");
scanf("%d",&com);
switch(com)
{
case ADD:
{
if(add_data(ls) == TRUE)
{
printf("添加成功!\n");
}
else
{
printf("添加失败!\n");
}
break;
}
case DISPLAY:
{
if(display_data(ls) != TRUE)
{
system("clear");
printf("通讯录目前无联系人\n");;
}
break;
}
case DELETE:
{
delete_flag = delete_data(ls);
if(delete_flag == TRUE)
{
printf("删除成功\n");
}
else if(delete_flag == FALSE)
{
printf("删除失败\n");
}
else if(delete_flag == ERROR)
{
printf("要删除的联系人不存在\n");
}
break;
}
case SEARCH:
{
search_flag = search_data(ls);
if(search_flag == TRUE)
{
printf("---------------------------------------------------\n");
}
if(search_flag != TRUE)
{
printf("未查找到该联系人\n");
}
break;
}
case CHANGE:
{
modify_flag = modify_data(ls);
if(modify_flag == TRUE)
{
printf("修改成功\n");
}
else if(modify_flag == FALSE)
{
printf("修改失败\n");
}
else if(modify_flag == ERROR)
{
printf("要修改的联系人不存在\n");
}
break;
}
case QUIT:
printf("谢谢使用!\n");
return ;
default:
printf("请输入正确的选项\n");
break;
}
}
}
main.c
#include
#include "Txl.h"
int main()
{
List *ls = Creat_List();
system("clear");
if(NULL == ls)
{
printf("创建通讯录失败\n");
}
else
{
printf("创建通讯录成功\n");
}
menu(ls);
Destroy_List(ls);
return 0;
}