|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct List
{
char name[40];
char phone[20];
struct List *next;
};
void addPerson(struct List **head);
struct List *findPerson(struct List *list,char *name);
struct List *changePerson(struct List *list);
void delPerson(struct List *list, char *name);
void displayContacts(struct List *list);
void freeList(struct List *list);
void addPerson(struct List **head)
{
struct List *list, *tail;
list = (struct List *)malloc(sizeof(struct List));
printf("请输入新联系人的姓名:");
scanf("%s", list->name);
printf("请输入新联系人的手机:");
scanf("%s", list->phone);
if(*head != NULL)
{
tail->next = list;
list->next = NULL;
}
else
{
*head = list;
list->next = NULL;
}
tail = list;
}
struct List *findPerson(struct List *list,char *name)
{
while(strcmp(list->name, name) && list != NULL)
{
list = list->next;
}
if(list == NULL)
{
printf("很抱歉,未找到匹配联系人\n");
}
else
{
return list;
}
}
struct List *changePerson(struct List *list)
{
printf("请输入新的姓名:");
scanf("%s", list->name);
printf("请输入新的手机:");
scanf("%s", list->phone);
return list;
}
void delPerson(struct List *list,char *name)
{
struct List *previous;
while(strcmp(list->name, name) && list !=NULL)
{
previous = list;
list = list->next;
}
if(list == NULL)
{
printf("很抱歉未找到删除目标\n");
return;
}
else
{
previous = list->next;
free(list);
printf("成功删除!\n");
}
return;
}
void displayContacts(struct List *list)
{
int num = 1;
while(list != NULL)
{
printf("第%d位联系人:\n", num);
printf("姓名:%s\n", list->name);
printf("手机:%s\n", list->phone);
list = list->next;
num++;
}
printf("打印完毕!\n");
}
void freeList(struct List *list)
{
struct List *temp;
while(list != NULL)
{
temp = list;
list = list->next;
free(temp);
}
}
int main(void)
{
printf("欢迎来到通讯录程序\n");
int o;
struct List *head;
do
{
printf("插入新的联系人(1)\n");
printf("查找已有联系人(2)\n");
printf("更改已有联系人(3)\n");
printf("删除已有联系人(4)\n");
printf("显示当前通讯录(5)\n");
printf("退出通讯录程序(6)\n");
printf("操作选择:");
scanf("%d", &o);
if(o == 1)
{
addPerson(&head);
printf("添加成功!\n");
}
if(o == 2)
{
char name1[40];
struct List *address1;
printf("请输入要查找的联系人名字:");
scanf("%s", name1);
address1 = findPerson(head,name1);
if(address1 == NULL)
{
printf("很抱歉未找到联系人\n");
}
else
{
printf("姓名:%s\n", address1->name);
printf("手机:%s\n", address1->phone);
}
}
if(o == 3)
{
char name2[40];
struct List *address2;
printf("请输入要更改联系人的姓名:");
scanf("%s", name2);
address2 = findPerson(head,name2);
if(address2 == NULL)
{
printf("很抱歉未找到联系人\n");
}
else
{
address2 = changePerson(address2);
printf("修改成功!\n");
printf("新姓名:%s\n", address2->name);
printf("新手机:%s\n", address2->phone);
}
}
if(o == 4)
{
char name3[40];
printf("请输入要删除的联系人姓名:");
scanf("%s", name3);
delPerson(head, name3);
}
if(o == 5)
{
displayContacts(head);
}
if(o == 6)
{
freeList(head);
exit(1);
}
}while(1);
return 0;
}
有大神帮我看一下我这代码为什么不可以吗?求求了,不知道问题出在哪里 |
|