特色功能单链表通讯录c语言
时间: 2023-08-25 15:26:34 浏览: 104
以下是一个简单的特色功能单链表通讯录的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义通讯录节点结构体
typedef struct AddressBookNode {
char name[20];
char phone[20];
char email[50];
struct AddressBookNode *next;
} AddressBookNode, *AddressBook;
// 初始化通讯录
void initAddressBook(AddressBook *book) {
*book = NULL;
}
// 添加联系人
void addContact(AddressBook *book) {
AddressBookNode *newNode = (AddressBookNode*)malloc(sizeof(AddressBookNode));
printf("请输入联系人姓名:");
scanf("%s", newNode->name);
printf("请输入联系人电话:");
scanf("%s", newNode->phone);
printf("请输入联系人邮箱:");
scanf("%s", newNode->email);
newNode->next = NULL;
if (*book == NULL) {
*book = newNode;
} else {
AddressBookNode *p = *book;
while (p->next != NULL) {
p = p->next;
}
p->next = newNode;
}
printf("联系人添加成功!\n");
}
// 删除联系人
void deleteContact(AddressBook *book) {
char name[20];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
AddressBookNode *p = *book;
AddressBookNode *prev = NULL;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
if (prev == NULL) {
*book = p->next;
} else {
prev->next = p->next;
}
free(p);
printf("联系人删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到该联系人!\n");
}
// 修改联系人
void modifyContact(AddressBook book) {
char name[20];
printf("请输入要修改的联系人姓名:");
scanf("%s", name);
AddressBookNode *p = book;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
printf("请输入新的联系人电话:");
scanf("%s", p->phone);
printf("请输入新的联系人邮箱:");
scanf("%s", p->email);
printf("联系人修改成功!\n");
return;
}
p = p->next;
}
printf("未找到该联系人!\n");
}
// 查找联系人
void searchContact(AddressBook book) {
char name[20];
printf("请输入要查找的联系人姓名:");
scanf("%s", name);
AddressBookNode *p = book;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
printf("联系人姓名:%s\n", p->name);
printf("联系人电话:%s\n", p->phone);
printf("联系人邮箱:%s\n", p->email);
return;
}
p = p->next;
}
printf("未找到该联系人!\n");
}
// 显示所有联系人
void showAllContacts(AddressBook book) {
AddressBookNode *p = book;
while (p != NULL) {
printf("联系人姓名:%s\n", p->name);
printf("联系人电话:%s\n", p->phone);
printf("联系人邮箱:%s\n", p->email);
p = p->next;
}
}
// 释放通讯录内存
void releaseAddressBook(AddressBook *book) {
AddressBookNode *p = *book;
while (p != NULL) {
AddressBookNode *tmp = p;
p = p->next;
free(tmp);
}
*book = NULL;
}
int main() {
AddressBook book;
initAddressBook(&book);
int choice;
do {
printf("请选择操作:\n");
printf("1. 添加联系人\n");
printf("2. 删除联系人\n");
printf("3. 修改联系人\n");
printf("4. 查找联系人\n");
printf("5. 显示所有联系人\n");
printf("6. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addContact(&book);
break;
case 2:
deleteContact(&book);
break;
case 3:
modifyContact(book);
break;
case 4:
searchContact(book);
break;
case 5:
showAllContacts(book);
break;
}
} while (choice != 6);
releaseAddressBook(&book);
return 0;
}
```
这个通讯录可以进行添加、删除、修改、查找和显示所有联系人的操作。其中使用单链表来存储联系人信息,每个节点包含姓名、电话、邮箱和下一个节点的地址。具体的实现细节可以参考代码中的注释。
阅读全文