写一串单链表通讯录管理系统的数据结构代码

时间: 2023-07-21 12:28:45 浏览: 54
好的,以下是一个简单的单链表通讯录管理系统的数据结构代码示例,供您参考: ```python class Contact: def __init__(self, name, phone_number): self.name = name self.phone_number = phone_number self.next = None class ContactBook: def __init__(self): self.head = None def add_contact(self, name, phone_number): new_contact = Contact(name, phone_number) if not self.head: self.head = new_contact else: current_contact = self.head while current_contact.next: current_contact = current_contact.next current_contact.next = new_contact def delete_contact(self, name): if not self.head: print("Contact book is empty.") elif self.head.name == name: self.head = self.head.next else: current_contact = self.head while current_contact.next and current_contact.next.name != name: current_contact = current_contact.next if not current_contact.next: print(f"{name} not found in contact book.") else: current_contact.next = current_contact.next.next def search_contact(self, name): if not self.head: print("Contact book is empty.") else: current_contact = self.head while current_contact and current_contact.name != name: current_contact = current_contact.next if not current_contact: print(f"{name} not found in contact book.") else: print(f"Name: {current_contact.name} Phone number: {current_contact.phone_number}") def display_contacts(self): if not self.head: print("Contact book is empty.") else: current_contact = self.head while current_contact: print(f"Name: {current_contact.name} Phone number: {current_contact.phone_number}") current_contact = current_contact.next ``` 这个数据结构包括两个类,`Contact` 和 `ContactBook`, `Contact` 类代表一个联系人,包含名字、电话号码和下一个联系人的指针;`ContactBook` 类代表整个通讯录,包含一个指向第一个联系人的头指针,以及添加、删除、查找和显示联系人等操作的方法。 希望这个示例代码可以帮助到您!

相关推荐

最新推荐

recommend-type

学生通讯录管理系统-数据结构课程设计.doc

学生通讯录管理系统-数据结构课程设计.doc 纸质通讯录已经不能满足大家的要求,容易丢失、查找困难等问题是纸质通讯录所有能克服的缺点。“学生通讯录管理系统”是为了帮助老师、同学等管理和分析的一种应用程序。
recommend-type

模拟通讯录-数据结构(顺序表实现基本功能).doc

模拟通讯录,设计相关数据结构,用菜单选择方式实现:通讯录的建立、通讯联系人的插入、删除、修改、查找等功能。
recommend-type

个人通讯录管理系统课程设计-C++.docx

计算机 电子信息工程 通信工程 实验 课程设计 工程项目 资源 必过 已过 好用 答辩简单 按着来就行 大学生关注我 以后所有我的课设都会更新 心得体会 参考文献自己写哈
recommend-type

Java通讯录管理系统

1:Java文本通讯录管理系统,不需要建立SQL数据库! 2:Java通讯录系统用到的都是一些简单易懂的代码,方便上手,适合初学者借鉴学习! 3:直接可以作为Java前面作业,或者想要有自己的内容稍微改改即可! 4:没有...
recommend-type

基于C语言实现个人通讯录管理系统

之前利用C语言完成了一个关于个人通讯录管理系统的课题,主要是关于联系人的添加、查找、删除、修改、输出以及文件的写入与读出,还有一个甜点功能—模拟通话,它的实现原理也很容易理解,文章末尾会介绍到。...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。