Linux c编写通讯录(链表和c库文件)

通讯录基本功能:增删改查
文件保存:c库文件/Linux文件操作
6
在这里插入图片描述
终端运行结果在这里插入图片描述
这只是部分实现代码,并非全!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言创建通讯录链表的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 50 #define MAX_PHONE_LEN 20 typedef struct Contact { char name[MAX_NAME_LEN]; char phone[MAX_PHONE_LEN]; struct Contact *next; } Contact; Contact *head = NULL; void addContact() { Contact *newContact = (Contact*) malloc(sizeof(Contact)); printf("Enter name: "); scanf("%s", newContact->name); printf("Enter phone number: "); scanf("%s", newContact->phone); newContact->next = NULL; if (head == NULL) { head = newContact; } else { Contact *current = head; while (current->next != NULL) { current = current->next; } current->next = newContact; } printf("Contact added successfully!\n"); } void displayContacts() { if (head == NULL) { printf("No contacts found.\n"); } else { Contact *current = head; printf("Contacts:\n"); while (current != NULL) { printf("%s: %s\n", current->name, current->phone); current = current->next; } } } void deleteContact() { if (head == NULL) { printf("No contacts found.\n"); } else { char name[MAX_NAME_LEN]; printf("Enter name of contact to delete: "); scanf("%s", name); Contact *current = head; Contact *prev = NULL; while (current != NULL) { if (strcmp(current->name, name) == 0) { if (prev == NULL) { head = current->next; } else { prev->next = current->next; } free(current); printf("Contact deleted successfully!\n"); return; } prev = current; current = current->next; } printf("Contact not found.\n"); } } int main() { int choice; do { printf("Menu:\n"); printf("1. Add contact\n"); printf("2. Display contacts\n"); printf("3. Delete contact\n"); printf("4. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: addContact(); break; case 2: displayContacts(); break; case 3: deleteContact(); break; case 4: printf("Exiting...\n"); break; default: printf("Invalid choice.\n"); } } while (choice != 4); return 0; } ``` 该程序使用链表来存储联系人的姓名和电话号码。用户可以选择添加、显示或删除联系人。添加联系人时,程序会提示用户输入姓名和电话号码,并将联系人添加到链表的末尾。显示联系人时,程序会遍历整个链表并输出每个联系人的姓名和电话号码。删除联系人时,程序会提示用户输入要删除的联系人的姓名,并从链表中删除该联系人。如果联系人不存在,则程序会显示“Contact not found.”的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值