当前位置: 首页 > news >正文

北邮22信通:你是不是在looking for……那串代码?(2)第三章单链表

相信有了第二章顺序表的基础,小伙伴们学习第三章链表应该会轻松一点吧

目录

类模板下的单链表

1.1书上干净完整代码(无增改、适合自己动手实验)

1.2对书上代码的完善和对一些问题的验证和解释代码

1.补全一个函数:

2.this指针:

3.关于printlist函数的一点说明:(增改后代码第117行)

4.getlength函数最后一步为什么是--cnt(增改后代码第136行):

5.增改后代码:

6.增改后代码效果图:

 7.增改后代码运行效果:


类模板下的单链表

1.1书上干净完整代码(无增改、适合自己动手实验)

下面是书上单链表实现通信录的干净完整代码,基本一字不差~  有需要的友友可以拿走~

#include <iostream>
using namespace std;class phonebook
{
private:int ID;string name;string phone;string group;
public:phonebook() {};phonebook(int ID, string name, string phone, string group){this->ID = ID;this->name = name;this->phone = phone;this->group = group;}void print(){cout << this->ID << " " << this->name << " "<< this->phone << " " << this->group << endl;}bool operator==(phonebook& p){return (p.ID == this->ID) ? true : false;}
};template<class temp>
struct node
{temp data;node* next;
};template <class temp>
class linklist
{
private:node<temp>* front;
public:linklist(){this->front = new node<temp>;this->front->next = nullptr;}linklist(temp a[], int n);~linklist();void printlist();int getlengh() {};node<temp>* get(int i);int locate(temp x);void insert(int i, temp x);temp del(int i);
};//头插法
template<class temp>
linklist<temp>::linklist(temp a[], int n)
{this->front = new node<temp>;this->front->next = NULL;for (int i = n - 1; i >= 0; i--){node<temp>* s = new node<temp>;s->data = a[i];s->next = this->front->next;this->front->next = s;}
}//尾插法
/*
template<class temp>
linklist<temp>::linklist(temp a[], int n)
{this->front = new node<temp>;node<temp>* r = this->front();for (int i = 0; i < n; i++){node<temp>* s = new node<temp>;s->data = a[i];r->next = s;r = s;}r->next = NULL;
}
*/template<class temp>
linklist<temp>::~linklist()
{node<temp>* p = this->front;while (p != NULL){this->front = p;p = p->next;delete front;}
}template<class temp>
void linklist<temp>::printlist()
{node<temp>* p = this->front->next;while (p != NULL){p->data.print();//数据域中的print方法(需要用户自定义)p = p->next;}cout << endl;
}//按位置查找,返回地址
template<class temp>
node<temp>* linklist<temp>::get(int i)
{node<temp>* p = this->front->next;int j = 1;while (p != NULL && j != i){p = p->next;j++;}return p;
}//按值查找,返回位置
template<class temp>
int linklist<temp>::locate(temp x)
{node<temp>* p = this->front->next;int j = 1;while (p != NULL){if (p->data == x)return j;p = p->next;j++;}return -1;//如果没有找到,返回无效值
}template <class temp>
void linklist<temp>::insert(int i, temp x)
{node<temp>* p = this->front;if (i != 1)p = get(i - 1);//get(i - 1)表示要插入的位置的前一个结点地址if (p != NULL){node<temp>* s = new node<temp>;s->data = x;s->next = p->next;p->next = s;}else{cout << "插入位置错误:" << endl;exit(0);}
}template<class temp>
temp linklist<temp>::del(int i)
{node<temp>* p = this->front;if (i != 1)p = get(i - 1);node<temp>* q = p->next;p->next = q->next;temp x = q->data;delete q;return x;
}int main()
{phonebook pbook[4] ={{20181208,"mary","13011221827","classmates"},{20181127,"tom","13934621123","family"},{20181156,"john","1324579880","classmates"},{20181133,"lisa","1378001822","teacher"}};phonebook record(20181209, "phoenix", "1590209020", "teacher");linklist<phonebook>list(pbook, 4);cout << "通信录内容列表:" << endl;list.printlist();list.insert(1, record);cout << "通信录内容列表:" << endl;list.printlist();phonebook x = list.del(3);cout << "删除元素:" << endl;x.print();cout << "通信录内容列表:" << endl;list.printlist();int p = list.locate(record);cout << "phoenix的位置是:" << p << endl;return 0;
}

代码效果图:

 

运行效果图:

1.2对书上代码的完善和对一些问题的验证和解释代码

(自己增改部分已经在代码中标明,有助于友友们对问题的理解)

1.补全一个函数:

        书上没有对getlength函数做定义,本代码已经补全;

2.this指针:

        “this->”,是个指针,p用没有,就是本人觉得写着顺手,看代码的时候直接忽略即可~

3.关于printlist函数的一点说明:(增改后代码第117行)

        为什么printlist函数是从this->front->next开始打印数据,而不是this->front呢?

        因为无论在显示构造函数还是隐式构造函数中,都没有对头结点的数据域赋值

        如果我们将this->front->next改成this->front:程序不会报错,但是会在头结点数据域输出的位置输出一串乱码。这代表头结点的存储地址。

        为了便于验证,我们在linklist中添加了getfront函数,用来输出头结点的数据域。

        (见增改后代码第51行函数声明)验证结果已在运行框中显示。

4.getlength函数最后一步为什么是--cnt(增改后代码第136行):

        其实有了说明3的解释,相信大家应该都能大概明白,这个顺序表的实际长度确实是cnt的,但是因为头结点默认不存放有效数据,所以考虑有效长度时不将其算在内。

5.增改后代码:

#include <iostream>
using namespace std;class phonebook
{
private:int ID;string name;string phone;string group;
public:phonebook() {};phonebook(int ID, string name, string phone, string group){this->ID = ID;this->name = name;this->phone = phone;this->group = group;}void print(){cout << this->ID << " " << this->name << " "<< this->phone << " " << this->group << endl;}bool operator==(phonebook& p){return (p.ID == this->ID) ? true : false;}
};template<class temp>
struct node
{temp data;node* next;
};template <class temp>
class linklist
{
private:node<temp>* front;
public:linklist(){this->front = new node<temp>;this->front->next = nullptr;}linklist(temp a[], int n);~linklist();temp getfront();//自己增加部分 书上无void printlist();int getlengh();node<temp>* get(int i);int locate(temp x);void insert(int i, temp x);temp del(int i);
};//头插法
template<class temp>
linklist<temp>::linklist(temp a[], int n)
{this->front = new node<temp>;this->front->next = NULL;for (int i = n - 1; i >= 0; i--){node<temp>* s = new node<temp>;s->data = a[i];s->next = this->front->next;this->front->next = s;}
}//尾插法
/*
template<class temp>
linklist<temp>::linklist(temp a[], int n)
{this->front = new node<temp>;node<temp>* r = this->front();for (int i = 0; i < n; i++){node<temp>* s = new node<temp>;s->data = a[i];r->next = s;r = s;}r->next = NULL;
}
*/template<class temp>
linklist<temp>::~linklist()
{node<temp>* p = this->front;while (p != NULL){this->front = p;p = p->next;delete front;}
}//自己增加部分 书上无
template<class temp>
temp linklist<temp>::getfront()
{node<temp>* p = front;temp x = p->data;return x;
}template<class temp>
void linklist<temp>::printlist()
{node<temp>* p = this->front->next;//见说明3.while (p != NULL){p->data.print();//数据域中的print方法(需要用户自定义)p = p->next;}cout << endl;
}template<class temp>
int linklist<temp>::getlengh()
{node<temp>* p = this->front;int cnt = 0;while (p != NULL){p = p->next;cnt++;}return --cnt;//见说明4
}//按位置查找,返回地址
template<class temp>
node<temp>* linklist<temp>::get(int i)
{node<temp>* p = this->front->next;int j = 1;while (p != NULL && j != i){p = p->next;j++;}return p;
}//按值查找,返回位置
template<class temp>
int linklist<temp>::locate(temp x)
{node<temp>* p = this->front->next;int j = 1;while (p != NULL){if (p->data == x)return j;p = p->next;j++;}return -1;//如果没有找到,返回无效值
}template <class temp>
void linklist<temp>::insert(int i, temp x)
{node<temp>* p = this->front;if (i != 1)p = get(i - 1);//get(i - 1)表示要插入的位置的前一个结点地址if (p != NULL){node<temp>* s = new node<temp>;s->data = x;s->next = p->next;p->next = s;}else{cout << "插入位置错误:" << endl;exit(0);}
}template<class temp>
temp linklist<temp>::del(int i)
{node<temp>* p = this->front;if (i != 1)p = get(i - 1);node<temp>* q = p->next;p->next = q->next;temp x = q->data;delete q;return x;
}int main()
{phonebook pbook[4] ={{20181208,"mary","13011221827","classmates"},{20181127,"tom","13934621123","family"},{20181156,"john","1324579880","classmates"},{20181133,"lisa","1378001822","teacher"}};phonebook record(20181209, "phoenix", "1590209020", "teacher");linklist<phonebook>list(pbook, 4);cout << "通信录内容列表:" << endl;list.printlist();//自己增加部分 书上无cout << "验证头结点数据无效:" << endl;phonebook y = list.getfront();cout << "头结点数据为:" << endl;y.print();cout << endl;list.insert(1, record);cout << "通信录内容列表:" << endl;list.printlist();phonebook x = list.del(3);cout << "删除元素:" << endl;x.print();cout << "通信录内容列表:" << endl;list.printlist();int p = list.locate(record);cout << "phoenix的位置是:" << p << endl;//自己增加部分 书上无cout << "通信录的长度为:" << endl;cout << list.getlengh();return 0;
}

6.增改后代码效果图:

 7.增改后代码运行效果:

 

上一篇文章:数据结构与算法  第二章 顺序表  请参考以下链接 ~

https://blog.csdn.net/bc202205/article/details/129311232?spm=1001.2014.3001.5501

写码不易,关注一下作者再走呗o(╥﹏╥)o

谢谢支持~

相关文章:

北邮22信通:你是不是在looking for……那串代码?(2)第三章单链表

相信有了第二章顺序表的基础&#xff0c;小伙伴们学习第三章链表应该会轻松一点吧 目录 类模板下的单链表 1.1书上干净完整代码&#xff08;无增改、适合自己动手实验&#xff09; 1.2对书上代码的完善和对一些问题的验证和解释代码 1.补全一个函数&#xff1a; 2.this指…...

蓝库云|告诉你传统产业该如何进行数字化转型

在后疫情时代下&#xff0c;企业该如何在面临生存危机的情形下&#xff0c;投入「数字化转型」、提升公司竞争力&#xff0c;已成为许多公司的当务之急&#xff0c;但到底什么是数字化转型呢&#xff1f;传统产业又如何着手进行数位转型&#xff1f; 数字化转型是什么&#xf…...

121.(leaflet篇)leaflet结合echarts4迁徙图

听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行完整代码包,运行如有问题,可“私信”博主。 效果如下所示: 下面献上完整代码,代码重要位置会做相应解释 <!DOCTYPE html> <html>...

链表及其基本操作

1.单链表&#xff1a;1.1定义/性质&#xff1a;链表是线性表的链式存储方式。单链表通过指针线性遍历&#xff0c;删除/增加节点时间复杂度为O(1&#xff09;,访问节点时间复杂度为O(n)。单链表分为带头结点和不带头结点两种&#xff0c;带头结点是为了方便统一操作&#xff08…...

【Java基础 下】 031 -- 反射 动态代理

一、什么是反射&#xff1f; 换句话说就是&#xff08;从类里拿出来&#xff09; 可以获取到&#xff1a;&#xff08;利用反射&#xff0c;我们可以获取到类中所有的东西&#xff09; 获取是先从class字节码文件中获取的 二、获取class对象的三种方式 三种方式也对应了三种阶段…...

springcloud3 GateWay

一 GateWay 1.1 GateWay的作用 gateway相当于所有服务的门户&#xff0c;将客户端请求与服务端应用相分离&#xff0c;客户端请求通过gateway后由定义的路由和断言进行转发&#xff0c;路由代表需要转发请求的地址&#xff0c;断言相当于请求这些地址时所满足的条件&#xff…...

万字长文:Stable Diffusion 保姆级教程

万字长文&#xff1a;Stable Diffusion 保姆级教程 2022年绝对是人工智能爆发的元年&#xff0c;前有 stability.ai 开源 Stable Diffusion 模型&#xff0c;后有 Open AI 发布 ChatGPT&#xff0c;二者都是里程碑式的节点事件&#xff0c;其重要性不亚于当年苹果发布iPhone&a…...

WAMP搭建靶场

WAMP W&#xff1a;windows A&#xff1a;apache M&#xff1a;mysql&#xff0c;mariadb P&#xff1a;php 1. 下载phpstudy Windows版phpstudy下载 - 小皮面板(phpstudy) 2. 安装phpstudy 默认安装即可 3. 下载DVWA靶场 https://github.com/digininja/DVWA/archive/…...

Uipath Excel 自动化系列13-ForEachExcelSheet(遍历Sheet)

活动描述 ForEachExcelSheet(遍历Sheet)&#xff1a;遍历Excel中的工作表&#xff0c;可以对 Excel 工作簿中的每个工作表重复一个或多个活动,该活动需与Use Excel File 活动选择的 Excel 文件一起使用。 使用场景&#xff1a;当处理包含多张工作表的 Excel 文件&#xff0c;…...

JDBC快速入门

&#x1f34e;道阻且长&#xff0c;行则将至。&#x1f353; 目录 一、JDBC入门 1.概述 (1)JDBC本质 (2)JDBC好处 2.快速入门 (1)步骤 (2)实践 (3)两个小问题 一、JDBC入门 1.概述 JDBC就是使用Java语言操作关系型数据库的一套API&#xff0c;全称&#xff1a;( Java…...

蓝桥杯三月刷题 第六天

文章目录&#x1f4a5;前言&#x1f609;解题报告&#x1f4a5;星期计算&#x1f914;一、思路:&#x1f60e;二、代码&#xff1a;&#x1f4a5;考勤刷卡&#x1f914;一、思路:&#x1f60e;二、代码&#xff1a;&#x1f4a5;卡片&#x1f914;一、思路:&#x1f60e;二、代…...

分享几个常用的运维 shell 脚本

今天咸鱼给大家分享几个不错的 Linux 运维脚本&#xff0c;这些脚本中大量使用了 Linux 的文本三剑客&#xff1a; awkgrepsed 建议大家这三个工具都要了解并最好能够较为熟练的使用 根据 PID 显示进程所有信息 根据用户输入的PID&#xff0c;过滤出该PID所有的信息 #! /b…...

分隔链表(精美图示详解哦)

全文目录引言分隔链表题目描述与思路实现总结引言 前面&#xff0c;我们熟悉了管理链表中的数据的方法&#xff0c;也了解了几道与链表相关的题目&#xff1a; 戳我看单链表详解哦 在本篇文章中&#xff0c;我们将再了解一道题目&#xff1a;分隔链表&#xff1a; 分隔链表OJ…...

腾讯乐固加固+app签名+多渠道打包

一、腾讯乐固-基础版免费加固-上传未加固的app-下载加固包&#xff08;加固成功会清除原apk的签名信息和多渠道信息&#xff09;https://console.cloud.tencent.com/ms/reinforce/list/basic二、使用AndroidStudio自带工具apksigner对apk重新签名找到apksigner.bat文件 路径D:\…...

Spring Boot整合Redis缓存(Lettuce)

spring-boot-demo-cache-redis 此 demo 主要演示了 Spring Boot 如何整合 redis&#xff0c;操作redis中的数据&#xff0c;并使用redis缓存数据。连接池使用 Lettuce。 Lettuce官网 pom.xml <!-- data-redis --> <dependency><groupId>org.springframework…...

Feign

而Feign则会完全代理HTTP请求&#xff0c;我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix&#xff0c;可以让我们不再需要显式地使用这两个组件。 Feign具有如下特性&#xff1a; 支持可插拔的HTTP编码器和解码器; 支持Hystrix和…...

【代码训练营】day54 | 392.判断子序列 115.不同的子序列

所用代码 java 判断子序列 LeetCode 392 题目链接&#xff1a;判断子序列 LeetCode 392 - 简单 思路 这题和之前求最长公共子序列一样。 dp[i] [j]&#xff1a;以i-1为结尾的字符串s 和 以j-1为结尾的字符串t 组成的相同子序列的长度 递推公式&#xff1a; 相等dp[i][j] d…...

【unity3D】创建TextMeshPro(TMP)中文字体(解决输入中文乱码问题)

&#x1f497; 未来的游戏开发程序媛&#xff0c;现在的努力学习菜鸡 &#x1f4a6;本专栏是我关于游戏开发的学习笔记 &#x1f236;本篇是unity的TMP中文输入显示乱码的解决方式 创建 TextMeshPro 中文字体遇到的问题描述解决方式Font Asset Creator 面板扩展中文字体文本遇到…...

JAVA开发(JAVA中的异常)

在java开发与代码运行过程中&#xff0c;我们经常会遇到需要处理异常的时候。有时候是在用编辑器写代码&#xff0c;点击保存的时候&#xff0c;编辑器就提示我们某块代码有异常&#xff0c;强制需要处理。有时候是我们启动&#xff0c;运行JAVA代码的时候的&#xff0c;日志里…...

lesson8-Linux多线程

Linux线程概念 线程在进程内部执行,是OS调度的基本单位OS是可以做到让进程进行资源的细粒度划分的物理内存是以4kb为单位的我们的.exe可执行程序本来就是按照地址空间的方式进行编译的页表映射 - 详细图 理解线程 线程在进程的地址空间内运行, 进程内部具有多个执行流的,而线程…...

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销&#xff0c;平衡网络负载&#xff0c;延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

DockerHub与私有镜像仓库在容器化中的应用与管理

哈喽&#xff0c;大家好&#xff0c;我是左手python&#xff01; Docker Hub的应用与管理 Docker Hub的基本概念与使用方法 Docker Hub是Docker官方提供的一个公共镜像仓库&#xff0c;用户可以在其中找到各种操作系统、软件和应用的镜像。开发者可以通过Docker Hub轻松获取所…...

为什么需要建设工程项目管理?工程项目管理有哪些亮点功能?

在建筑行业&#xff0c;项目管理的重要性不言而喻。随着工程规模的扩大、技术复杂度的提升&#xff0c;传统的管理模式已经难以满足现代工程的需求。过去&#xff0c;许多企业依赖手工记录、口头沟通和分散的信息管理&#xff0c;导致效率低下、成本失控、风险频发。例如&#…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

算法岗面试经验分享-大模型篇

文章目录 A 基础语言模型A.1 TransformerA.2 Bert B 大语言模型结构B.1 GPTB.2 LLamaB.3 ChatGLMB.4 Qwen C 大语言模型微调C.1 Fine-tuningC.2 Adapter-tuningC.3 Prefix-tuningC.4 P-tuningC.5 LoRA A 基础语言模型 A.1 Transformer &#xff08;1&#xff09;资源 论文&a…...

Spring是如何解决Bean的循环依赖:三级缓存机制

1、什么是 Bean 的循环依赖 在 Spring框架中,Bean 的循环依赖是指多个 Bean 之间‌互相持有对方引用‌,形成闭环依赖关系的现象。 多个 Bean 的依赖关系构成环形链路,例如: 双向依赖:Bean A 依赖 Bean B,同时 Bean B 也依赖 Bean A(A↔B)。链条循环: Bean A → Bean…...

解决:Android studio 编译后报错\app\src\main\cpp\CMakeLists.txt‘ to exist

现象&#xff1a; android studio报错&#xff1a; [CXX1409] D:\GitLab\xxxxx\app.cxx\Debug\3f3w4y1i\arm64-v8a\android_gradle_build.json : expected buildFiles file ‘D:\GitLab\xxxxx\app\src\main\cpp\CMakeLists.txt’ to exist 解决&#xff1a; 不要动CMakeLists.…...

Elastic 获得 AWS 教育 ISV 合作伙伴资质,进一步增强教育解决方案产品组合

作者&#xff1a;来自 Elastic Udayasimha Theepireddy (Uday), Brian Bergholm, Marianna Jonsdottir 通过搜索 AI 和云创新推动教育领域的数字化转型。 我们非常高兴地宣布&#xff0c;Elastic 已获得 AWS 教育 ISV 合作伙伴资质。这一重要认证表明&#xff0c;Elastic 作为 …...

js 设置3秒后执行

如何在JavaScript中延迟3秒执行操作 在JavaScript中&#xff0c;要设置一个操作在指定延迟后&#xff08;例如3秒&#xff09;执行&#xff0c;可以使用 setTimeout 函数。setTimeout 是JavaScript的核心计时器方法&#xff0c;它接受两个参数&#xff1a; 要执行的函数&…...