代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 1024
struct node
{
int id;
char name[MAX_SIZE];
char phone[MAX_SIZE];
char address[MAX_SIZE];
struct node *next;
};
typedef struct node Node;
typedef Node *Link;
int CreatBook(Link *head)
{
*head = (Link)malloc(sizeof(Node));
(*head) -> next = NULL;
return 1;
}
void DisplayBook()
{
printf("\n\n");
printf("\t\t 通讯录\t\n");
printf("\t\t|---------------------------------|\n");
printf("\t\t| |\n");
printf("\t\t| [1] 插入联系人的信息 |\n");
printf("\t\t| [2]查找联系人的信息 |\n");
printf("\t\t| [3]删除联系人的信息 |\n");
printf("\t\t| [4]修改联系人的信息 |\n");
printf("\t\t| [5]输出所有联系人信息 |\n");
printf("\t\t| [0]退出 |\n");
printf("\t\t| |\n");
printf("\t\t| 如未建立新表,请先建立! |\n");
printf("\t\t| |\n");
printf("\t\t|---------------------------------|\n");
printf("\n");
printf("请选择(0-5):");
}
int InsertBook(Link *head,Link *newnode)
{
/*
if(0 == CreatBook(&head))
{
printf("ERROR!");
return 0;
}
*/
static int ID = 0;
*newnode = malloc(sizeof(Node));
Link temp = *head;
printf("录入的姓名;");
scanf("%s",(*newnode) ->name);
printf("\n");
printf("录入的电话;");
scanf("%s",(*newnode) -> phone);
printf("\n");
printf("录入家庭住址:");
scanf("%s",(*newnode) -> address);
printf("\n");
ID++;
(*newnode) -> id = ID;
if((*head) -> next == NULL)
{
(*head) -> next = *newnode;
(*newnode) -> next =NULL;
}
else
{
while(temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = (*newnode);
(*newnode) -> next = NULL;
}
}
int PrintBook(Link *head)
{
if((*head) -> next == NULL)
{
return -1;
}
Link temp = (*head) -> next;
printf("================================================ \n");
printf(" ID 姓名 电话 家庭住址 \n");
while(temp != NULL)
{
printf("%5d %5s %15s %15s\n",temp -> id,temp -> name,temp -> phone,temp -> address);
temp = temp -> next;
}
return 1;
}
int SearchBook(Link head ,Link newnode)
{
if(head -> next == NULL)
{
return 0;
}
Link temp = head;
int i = 0;
while(temp != NULL)
{
if(0 == strcmp((temp -> name),(newnode -> name)))
{
printf("%5d %5s %5s %5s\n",temp -> id,temp -> name,temp -> phone,temp -> address);
i++;
}
temp = temp -> next;
}
if(i > 0)
{
return 1;
}
else
{
return 0;
}
}
int DelBook(Link *head,char *name)
{
if((*head) -> next == NULL)
{
return 0;
}
char choose;
printf("是否删除此人!(y/ n)");
scanf("%s",&choose);
getchar();
if(choose == 'n')
{
return 2;
}
else
{
Link temp = *head;
Link ptr = temp;
temp = temp -> next;
while(temp != NULL)
{
if(0 == (strcmp((temp -> name),name)))
{
ptr -> next = temp -> next;
free(temp);
temp = NULL;
return 1;
break;
}
ptr = temp;
temp = temp -> next;
}
}
return 0;
}
int ChangeBook(Link *head,Link *newnode,Link *test)
{
if((*head) -> next == NULL)
{
return 0;
}
Link temp = *head;
Link del_pri = *test;
Link del_now = *newnode;
while(temp != NULL)
{
if(0 == strcmp((temp -> name),((*test) -> name)))
{
strcpy((temp -> name) , ((*newnode) -> name));
strcpy((temp -> phone), ((*newnode) -> phone));
strcpy((temp -> address),((*newnode) -> address));
free(del_pri);
free(del_now);
del_now = NULL;
del_pri = NULL;
return 1;
}
temp = temp -> next;
}
return 0;
}
int SortBook(Link head,Link *head_tmp)
{
if(head -> next == NULL)
{
return 0;
}
int ID_temp = 0;
int compare[26];
Link newnode;
int i;
Link temp = head -> next;
Link temp_head = *head_tmp;
for(i = 0;i < 26;i++)
{
compare[i] = 97 + i;
while(temp != NULL)
{
if(compare[i] == (int)(*(temp -> name)))
{
newnode = (Link)malloc(sizeof(Node));
ID_temp++;
newnode -> id = ID_temp;
strcpy((newnode -> name),(temp -> name));
strcpy((newnode -> phone),(temp -> phone));
strcpy((newnode -> address),(temp -> address));
while(temp_head -> next != NULL)
{
temp_head = temp_head -> next;
}
temp_head -> next = newnode;
newnode -> next = NULL;
}
temp = temp -> next;
}
temp = head -> next;
}
ID_temp = 0;
return 1;
}
void OpreatBook(Link head)
{
int n;
Link newnode;
DisplayBook();
scanf("%d",&n);
printf("\n");
switch(n)
{
case 0:
{
printf("谢 谢 使 用 通 讯 录 \n");
exit(0);
}
case 1:
{
InsertBook(&head,&newnode);
while(1)
{
char choose;
printf("是否继续添加(y / n):");
getchar();
scanf("%c",&choose);
if('y' == choose)
{
InsertBook(&head,&newnode);
}
else
{
break;
}
}
break;
}
case 2:
{
newnode = (Link)malloc(sizeof(Node));
Link temp = newnode;
printf("输入查找的姓名:");
scanf("%s",newnode -> name);
printf("=======================================\n");
printf(" ID 姓名 电话 家庭住址 \n");
if(!SearchBook(head,newnode))
{
printf("\n");
printf("\n");
printf("查无此人\n");
}
else
{
printf("\n");
printf("\n");
printf("查找成功\n");
}
free(temp);
temp = NULL;
break;
}
case 3:
{
char name[20];
int data;
printf("请输入你要删除的姓名:");
scanf("%s",name);
getchar();
data = DelBook(&head,name);
if(0 == data)
{
printf("查无此人,删除失败!\n");
}
else
{
if(2 == data)
{
break;
}
else
{
printf("删除成功\n");
}
}
break;
}
case 4:
{
Link test;
newnode = (Link)malloc(sizeof(Node));
test = (Link)malloc(sizeof(Node));
printf("输入需要修改人的信息:");
scanf("%s",test -> name);
printf("新修改的名字:");
scanf("%s",newnode -> name);
printf("新修改的电话:");
scanf("%s",newnode -> phone);
printf("新修改的家庭地址:");
scanf("%s",newnode -> address);
if(ChangeBook(&head,&newnode,&test) == 0)
{
printf("查无此人,修该不成功!");
}
else
{
/*
printf("新修改的姓名:");
scanf("%s",newnode -> name);
printf("新修改的电话:");
scanf("%s",newnode -> phone);
printf("新修改的家庭住址:");
scanf("%s",newnode -> address);
ChangeBook(&head,&newnode,&test);
*/
printf("修改成功!");
}
break;
}
case 5:
{
Link head_tmp;
CreatBook(&head_tmp);
Link temp = head_tmp;
if(SortBook(head,&head_tmp))
{
PrintBook(&head_tmp);
}
else
{
printf("通讯录为空,显示不成功!");
}
break;
}
default:
{
printf("输入选项错误,请重新输入!");
break;
}
}
}
int main()
{
Link head;
CreatBook(&head);
while(1)
{
OpreatBook(head);
}
return 0;
}
结果截图: