当前位置:实例文章 » 其他实例» [文章]C语言--通讯录的实现

C语言--通讯录的实现

发布人:shili8 发布时间:2025-02-25 15:39 阅读次数:0

**通讯录的实现**

在这个项目中,我们将设计一个通讯录系统,允许用户添加、删除、修改联系人信息,并能够查询特定联系人的信息。

###1. 通讯录结构我们将使用一个链表来存储联系人的信息,每个结点代表一个联系人。每个联系人包含以下信息:

* 名字(name)
* 手机号码(phone_number)
* 电子邮件(email)

ctypedef struct Contact {
 char name[50];
 char phone_number[20];
 char email[100];
} Contact;


###2. 通讯录链表结构我们将使用一个链表来存储联系人的信息,每个结点代表一个联系人。

ctypedef struct Node {
 Contact contact;
 struct Node* next;
} Node;


###3. 通讯录类结构我们将设计一个通讯录类,包含以下方法:

* `add_contact()`: 添加新联系人* `delete_contact()`: 删除指定联系人* `modify_contact()`: 修改指定联系人的信息* `search_contact()`: 查询指定联系人的信息
ctypedef struct ContactList {
 Node* head;
} ContactList;


###4. 通讯录类方法实现#### add_contact()

添加新联系人到通讯录中。

cvoid add_contact(ContactList* list, char name[50], char phone_number[20], char email[100]) {
 Node* new_node = (Node*)malloc(sizeof(Node));
 strcpy(new_node->contact.name, name);
 strcpy(new_node->contact.phone_number, phone_number);
 strcpy(new_node->contact.email, email);
 new_node->next = NULL;

 if (list->head == NULL) {
 list->head = new_node;
 } else {
 Node* current = list->head;
 while (current->next != NULL) {
 current = current->next;
 }
 current->next = new_node;
 }
}


#### delete_contact()

删除指定联系人。

cvoid delete_contact(ContactList* list, char name[50]) {
 if (list->head == NULL) return;

 if (strcmp(list->head->contact.name, name) ==0) {
 Node* temp = list->head;
 list->head = list->head->next;
 free(temp);
 return;
 }

 Node* current = list->head;
 while (current->next != NULL) {
 if (strcmp(current->next->contact.name, name) ==0) {
 Node* temp = current->next;
 current->next = current->next->next;
 free(temp);
 return;
 }
 current = current->next;
 }
}


#### modify_contact()

修改指定联系人的信息。

cvoid modify_contact(ContactList* list, char name[50], char new_name[50], char new_phone_number[20], char new_email[100]) {
 if (list->head == NULL) return;

 if (strcmp(list->head->contact.name, name) ==0) {
 strcpy(list->head->contact.name, new_name);
 strcpy(list->head->contact.phone_number, new_phone_number);
 strcpy(list->head->contact.email, new_email);
 return;
 }

 Node* current = list->head;
 while (current->next != NULL) {
 if (strcmp(current->next->contact.name, name) ==0) {
 strcpy(current->next->contact.name, new_name);
 strcpy(current->next->contact.phone_number, new_phone_number);
 strcpy(current->next->contact.email, new_email);
 return;
 }
 current = current->next;
 }
}


#### search_contact()

查询指定联系人的信息。

cvoid search_contact(ContactList* list, char name[50]) {
 if (list->head == NULL) {
 printf("No contact found.
");
 return;
 }

 Node* current = list->head;
 while (current != NULL) {
 if (strcmp(current->contact.name, name) ==0) {
 printf("Name: %s
Phone Number: %s
Email: %s
", current->contact.name, current->contact.phone_number, current->contact.email);
 return;
 }
 current = current->next;
 }

 printf("No contact found.
");
}


###5. 主函数
cint main() {
 ContactList list;
 list.head = NULL;

 add_contact(&list, "John Doe", "1234567890", "john@example.com");
 add_contact(&list, "Jane Doe", "9876543210", "jane@example.com");

 search_contact(&list, "John Doe");

 modify_contact(&list, "John Doe", "John Smith", "1112223333", "john.smith@example.com");

 search_contact(&list, "John Smith");

 delete_contact(&list, "Jane Doe");

 search_contact(&list, "Jane Doe");

 return0;
}


这个项目实现了一个基本的通讯录系统,允许用户添加、删除、修改联系人信息,并能够查询特定联系人的信息。

相关标签:c语言开发语言
其他信息

其他资源

Top