1.单链表的基本操作及应用 ①创建 ②插入 ③删除 ④查找 ⑤显示 通讯录设计(应用)

时间: 2024-01-01 10:06:55 浏览: 32
好的,以下是单链表的基本操作及应用的实现思路: 1. 创建单链表 创建单链表需要定义一个链表节点结构体,包含数据域和指针域。然后通过循环创建节点并将它们链接起来,最后返回链表的头指针。 ``` struct Node { int data; // 数据域 Node* next; // 指针域 }; Node* createList() { Node* head = new Node; // 头节点不存储数据 head->next = nullptr; Node* tail = head; // 尾指针,初始指向头节点 int n; // 节点个数 cin >> n; for (int i = 0; i < n; i++) { Node* node = new Node; cin >> node->data; node->next = nullptr; tail->next = node; tail = node; } return head; } ``` 2. 插入节点 插入节点分为头插法和尾插法。头插法将新节点插入到链表头部,尾插法将新节点插入到链表尾部。 ``` // 头插法 void insertHead(Node* head, int value) { Node* node = new Node; node->data = value; node->next = head->next; head->next = node; } // 尾插法 void insertTail(Node* head, int value) { Node* tail = head; while (tail->next != nullptr) { tail = tail->next; } Node* node = new Node; node->data = value; node->next = nullptr; tail->next = node; } ``` 3. 删除节点 删除节点需要先找到待删除节点的前一个节点,然后将前一个节点的指针域指向待删除节点的后一个节点,最后释放待删除节点的内存空间。 ``` void deleteNode(Node* head, int value) { Node* p = head; while (p->next != nullptr && p->next->data != value) { p = p->next; } if (p->next != nullptr) { Node* temp = p->next; p->next = temp->next; delete temp; } } ``` 4. 查找节点 查找节点需要遍历链表,直到找到目标节点或到达链表尾部。 ``` Node* searchNode(Node* head, int value) { Node* p = head->next; while (p != nullptr && p->data != value) { p = p->next; } return p; } ``` 5. 显示链表 显示链表需要遍历链表,依次输出每个节点的数据。 ``` void displayList(Node* head) { Node* p = head->next; while (p != nullptr) { cout << p->data << " "; p = p->next; } cout << endl; } ``` 6. 通讯录设计(应用) 通讯录可以使用单链表来实现,每个节点存储一个人的姓名和电话号码。具体实现过程如下: ``` // 通讯录节点结构体 struct Person { string name; // 姓名 string phone; // 电话号码 Person* next; // 指针域 }; // 创建通讯录 Person* createAddressBook() { Person* head = new Person; head->next = nullptr; Person* tail = head; int n; cin >> n; for (int i = 0; i < n; i++) { Person* person = new Person; cin >> person->name >> person->phone; person->next = nullptr; tail->next = person; tail = person; } return head; } // 插入联系人 void insertPerson(Person* head, string name, string phone) { Person* person = new Person; person->name = name; person->phone = phone; person->next = head->next; head->next = person; } // 删除联系人 void deletePerson(Person* head, string name) { Person* p = head; while (p->next != nullptr && p->next->name != name) { p = p->next; } if (p->next != nullptr) { Person* temp = p->next; p->next = temp->next; delete temp; } } // 查找联系人 Person* searchPerson(Person* head, string name) { Person* p = head->next; while (p != nullptr && p->name != name) { p = p->next; } return p; } // 显示通讯录 void displayAddressBook(Person* head) { Person* p = head->next; while (p != nullptr) { cout << p->name << "\t" << p->phone << endl; p = p->next; } } ``` 以上是单链表的基本操作及通讯录应用的实现思路,具体实现过程中还需要考虑一些细节问题,例如输入格式检查、异常处理等。希望能够对您有所帮助。

相关推荐

最新推荐

recommend-type

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

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

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

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

基于Android手机通讯录的设计与实现_2018.doc

基于Android手机通讯录的设计与实现,基于Android手机通讯录的设计与实现
recommend-type

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

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

C语言课程设计 手机通讯录 管理系统

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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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