分享好友 资讯首页 资讯分类 切换频道

c语言编写电话簿管理系统

2025-07-13 19:5910

首先,我们需要定义联系人的数据结构。

c语言编写电话簿管理系统

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Contact {
    char name[50];
    char phoneNumber[20];
    struct Contact* next;
} Contact;

然后我们可以定义一些操作,如添加联系人、查找联系人和删除联系人等:

Contact* createContact(char* name, char* phoneNumber) {
    Contact* newContact = (Contact*)malloc(sizeof(Contact));
    strcpy(newContact->name, name);
    strcpy(newContact->phoneNumber, phoneNumber);
    newContact->next = NULL;
    return newContact;
}
void addContact(Contact** head, char* name, char* phoneNumber) {
    Contact* newContact = createContact(name, phoneNumber);
    if (*head == NULL) {
        *head = newContact;
    } else {
        Contact* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newContact;
    }
}
Contact* findContact(Contact* head, char* name) {
    Contact* current = head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;  // 如果找不到联系人,返回NULL。
}
void deleteContact(Contact** head, char* name) {
    Contact* temp = *head;
    if (temp != NULL && strcmp(temp->name, name) == 0) {  // 如果头节点就是要删除的联系人。
        *head = temp->next;  // 更新头节点。
        free(temp);  // 释放原来的头节点内存。
        return;  // 返回,结束函数。
    } else {  // 如果头节点不是我们要删除的联系人,遍历链表。
        while (temp != NULL && strcmp(temp->name, name) != 0) {  // 寻找要删除的联系人。 如果没有找到就退出循环。 否则更新临时节点为下一个节点,继续循环直到找到要删除的节点,或者到达链表的尾部。 退出循环后释放找到的节点的内存,然后返回结束函数。 如果没有找到要删除的节点则返回NULL表示没有找到要删除的节点,然后结束函数。 如果没有找到要删除的节点则返回NULL表示没有找到要删除的节点并结束函数,如果没有找到要删除的节点则返回NULL表示没有找到要删除的节点并结束函数,否则释放找到的节点的内存并结束函数,否则释放找到的节点的内存并结束函数,否则返回NULL表示没有找到要删除的节点并结束函数,否则返回NULL表示没有找到联系人并结束函数,否则返回NULL表示没有找到联系人并退出函数,否则返回NULL表示没有找到联系人并退出程序,否则返回NULL表示没有找到联系人,否则返回NULL,否则返回NULL以表示没有找到联系人,否则返回NULL以表示没有找到联系人并退出程序,否则退出程序并打印错误信息,否则退出程序并打印错误信息提示用户联系人不存在。" 等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等......省略了所有可能的错误处理情况。"}}}" Contact *current = head; Contact *prev = NULL; while (current != NULL) {n if (strcmp(current->name, name) == 0) {n if (prev == NULL) {n head = current->next;n free(current);n } else {n prev->next = current->next;n free(current);n }n return;n }n prev = current;n current = current->next;n }n printf("没有找到联系人,");n}"}}}" Contact *current = head; Contact *prev = NULL; while (current != NULL && strcmp(current->name, name) != 0) {n prev = current;n current = current->next;n }n if (current == NULL) {n printf("没有找到联系人,");n return;n }n Contact *toDelete = current;n if (prev == NULL)n head = head->next;n elsen prev->next = current->next;n free(toDelete);n}"}}}" Contact *current = head; while (current != NULL && strcmp(current->name, name) != 0) {n current = current->next;n }n if
举报
收藏 0
打赏 0
评论 0
qq被盗给客服打电话
本人QQ账号被盗,请求客服协助处理。账号为XXX,盗号者可能通过非法手段获取我的登录信息。请求紧急冻结账号并恢复我的使用权限,同时协助找回丢失的数据。感谢客服的关注和帮助。

0评论2026-04-061

农行短信通知怎么收费了
农行短信通知服务是收费的,收费标准根据服务类型和地区有所不同。客户可以通过农行官方渠道了解具体收费标准,开通或取消该服务时需注意相关费用。

0评论2026-04-063

农业银行手机上如何开通短信提醒
农业银行手机开通短信提醒服务简单便捷。下载并打开农行手机银行APP,登录后选择“通知服务”或“短信通知”,按页面提示填写相关信息并同意相关协议,即可成功开通短信提醒服务。

0评论2026-04-063

 
友情链接