【C语言】通讯录系统设计中遇到的问题
各位大佬好!我用C语言编写了个通讯录系统,系统的要求就是小甲鱼视频当中的要求(见下图)。目前在删除通讯录这边碰到了bug。删除通讯录当中的问题就是当我执行删除操作后,整个主函数会直接结束,不知道为什么,还请各位大佬麻烦看看。我的删除函数:
void delPerson(struct Address_book **head) { //删除指令
char name;
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");//函数走到了最后,说明没能遍历到
}
其中Address_book是我创建的结构体,用于存放联系人姓名(char类型数组)和联系人电话(long long类型):
struct Address_book {
char name;
long long phone_number;
struct Address_book *next;
};
主函数:
int main(void) {
int input;
char name;
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;
}
最后是整个代码,为了方便看我把最主要要解决的部分放在上面了,怕有其他问题,所以就把整个代码放最后了
#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;
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;
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;
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;
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;
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;
}
} 本帖最后由 superbe 于 2020-9-8 21:52 编辑
删除函数改成:
void delPerson(struct Address_book **head) { //删除指令
char name;
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
struct Address_book *previous = NULL; //前一个指针
struct Address_book *current = *head; //当前要删除的指针
while (current != NULL) { //判断当前节点是否为NULL
if (!strcmp(current->name, name)) { //定位完毕,删除,结束
if (previous == NULL) //头节点即是要删除的节点的情况
*head = current->next;
else
previous->next = current->next;
free(current);
return;
}
previous = current;
current = current->next;
}
printf("未找到该联系人!\n");//函数走到了最后,说明没能遍历到
}
另外,主函数调用freeContacts()漏掉了参数,改成:
freeContacts(head);
页:
[1]