马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大佬好!我用C语言编写了个通讯录系统,系统的要求就是小甲鱼视频当中的要求(见下图)。目前在删除通讯录这边碰到了bug。删除通讯录当中的问题就是当我执行删除操作后,整个主函数会直接结束,不知道为什么,还请各位大佬麻烦看看。
小甲鱼的要求:
全部代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Address_book;
void addPreson(struct Address_book **);
void findPerson(struct Address_book *);
void changePerson(struct Address_book *);
void delPerson(struct Address_book **);
void displayContacts(struct Address_book *);
void freeContacts(struct Address_book *);
int main(void) {
int input;
char name[40];
long long phone_number;
struct Address_book *head = NULL;//头指针
do {
printf("请输入操作号码:\n"
"1.插入新的联系人\t2.查找已有联系人\t3.更改已有联系人\n"
"4.删除已有联系人\t5.显示当前通讯录\t6.退出通讯录");
putchar('\n');
scanf("%d", &input);
if (input < 1 || input > 6) {
printf("非法输入!");
continue;
}
switch (input) {
case 1 :
addPreson(&head);
break;
case 2:
findPerson(head);
break;
case 3:
changePerson(head);
break;
case 4:
delPerson(&head);
break;
case 5:
displayContacts(head);
break;
case 6:
freeContacts();
break;
}
} while (input != 6);
return 0;
}
struct Address_book {
char name[40];
long long phone_number;
struct Address_book *next;
};
void addPreson(struct Address_book **head) { //添加
struct Address_book *previous;
struct Address_book *new;
new = (struct Address_book *) malloc(sizeof(struct Address_book));
if (new == NULL) {
printf("分配新空间失败,请重试!");
exit(1);
}
//给new赋值
printf("请输入添加的联系人姓名:");
scanf("%s", new->name);
printf("请输入添加的联系人电话:");
scanf("%lld", &new->phone_number);
if (*head != NULL) {
previous = *head;
//定义单链表尾部位置
while (previous->next != NULL) {
previous = previous->next;
}
//插入数据
previous->next = new;
new->next = NULL;
} else {
*head = new;
new->next = NULL;
}
}
void findPerson(struct Address_book *head) { //查找
char name[40];
printf("请输入你想查找的联系人姓名:");
scanf("%s", name);
struct Address_book *object;
object = NULL;
while (head != NULL) {
if (!strcmp(head->name, name)) {
object = head;
printf("查询完毕!\n");
printf("联系人姓名:%s\n", name);
printf("联系人电话%lld\n", object->phone_number);
return;
}
head = head->next;
}
printf("未找到该联系人!\n");
}
void changePerson(struct Address_book *head) { //替换
char name_1[40];
printf("请输入替换人的姓名:");
scanf("%s", name_1);
while (head != NULL) {
if (!strcmp(name_1, head->name)) {
printf("请输入替换后的姓名:");
scanf("%s", head->name);
printf("请输入替换后的电话:");
scanf("%lld", &head->phone_number);
printf("替换成功!\n");
return;
}
head = head->next;
}
printf("未找到该联系人!\n");
}
void delPerson(struct Address_book **head) { //删除指令
char name[40];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
struct Address_book *previous = NULL; //前一个指针
struct Address_book *current = *head; //当前要删除的指针
while ((*head) != NULL) {
if (!strcmp(current->name, name)) { //定位完毕,删除,结束
previous->next = current->next;
free(current);
return;
}
previous = current;
current = current->next;
}
printf("未找到该联系人!\n");//函数走到了最后,说明没能遍历到
}
void displayContacts(struct Address_book *head) {
struct Address_book *current;
current = head;
if (current == NULL) {
printf("通讯录为空!");
}
while (current != NULL) {
printf("姓名:%s\n", current->name);
printf("电话:%lld\n", current->phone_number);
current = current->next;
}
putchar('\n');
}
void freeContacts(struct Address_book *head) {
struct Address_book *temp;
while (head != NULL) {
temp = head;
free(head);
head = temp->next;
}
}
抱歉我说错了,删除函数改成这样 void delPerson(struct Address_book **head) { //删除指令
char name[40];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
struct Address_book *previous = *head; //前一个指针
struct Address_book *current = *head; //当前要删除的指针
while ((*head) != NULL) {
if (!strcmp(current->name, name)) { //定位完毕,删除,结束
previous->next = current->next;
free(current);
return;
}
current = current->next;
}
printf("未找到该联系人!\n");//函数走到了最后,说明没能遍历到
}
另外你的56行释放函数也要改一下 freeContacts(head); // 不放参数?
|