定义一个包含联系人信息(姓名、职业、电话号码、电子邮件、通讯地址)的链表,读入相应的联系人数据来完成通讯录的创建,同时逐行输出每个的联系人的信息。
时间: 2023-04-16 20:04:56 浏览: 45
可以定义一个结构体来存储联系人信息,然后使用链表来组织这些结构体。具体实现可以参考以下代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义联系人结构体
typedef struct Contact {
char name[20]; // 姓名
char job[20]; // 职业
char phone[20]; // 电话号码
char email[30]; // 电子邮件
char address[50]; // 通讯地址
struct Contact *next; // 指向下一个联系人的指针
} Contact;
// 创建新的联系人节点
Contact *createContact(char *name, char *job, char *phone, char *email, char *address) {
Contact *newContact = (Contact *)malloc(sizeof(Contact));
strcpy(newContact->name, name);
strcpy(newContact->job, job);
strcpy(newContact->phone, phone);
strcpy(newContact->email, email);
strcpy(newContact->address, address);
newContact->next = NULL;
return newContact;
}
// 添加联系人到链表中
void addContact(Contact **head, Contact *newContact) {
if (*head == NULL) {
*head = newContact;
} else {
Contact *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newContact;
}
}
// 读取联系人信息并创建链表
void readContacts(Contact **head) {
char name[20], job[20], phone[20], email[30], address[50];
while (1) {
printf("请输入联系人姓名(输入exit结束):");
scanf("%s", name);
if (strcmp(name, "exit") == ) {
break;
}
printf("请输入联系人职业:");
scanf("%s", job);
printf("请输入联系人电话号码:");
scanf("%s", phone);
printf("请输入联系人电子邮件:");
scanf("%s", email);
printf("请输入联系人通讯地址:");
scanf("%s", address);
Contact *newContact = createContact(name, job, phone, email, address);
addContact(head, newContact);
}
}
// 输出链表中所有联系人的信息
void printContacts(Contact *head) {
Contact *current = head;
while (current != NULL) {
printf("姓名:%s\n", current->name);
printf("职业:%s\n", current->job);
printf("电话号码:%s\n", current->phone);
printf("电子邮件:%s\n", current->email);
printf("通讯地址:%s\n", current->address);
printf("\n");
current = current->next;
}
}
int main() {
Contact *head = NULL;
readContacts(&head);
printContacts(head);
return ;
}
```
运行程序后,可以依次输入每个联系人的信息,输入exit结束输入。然后程序会输出每个联系人的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)