数据冒险之单链表

2023-12-27 09:08
文章标签 数据 单链 冒险

本文主要是介绍数据冒险之单链表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

定义链表的结点:Node.h

#ifndef NODE_H
#define NODE_Hclass Node
{
public:int data;Node *next;void printNode();
};
#endif 

打印结点数据:Node.cpp

#include"Node.h"
#include<iostream>
using namespace std;void Node::printNode()
{cout << data << endl;
}

List.h

/*单链表*/
#ifndef LIST_H
#define LIST_H#include"Node.h"
class List
{
public:List();										    //创建线性表 ~List();                                        //销毁线性表 void ClearList();                               //清空 bool ListEmpty();                                //判空 int  ListLength();                               //获取线性表长度 bool GetElem(int i, Node *pNode);                //获取指定元素 int LocateElem(Node *pNode);                         //定位元素 寻找第一个满足e的元素的位序 bool PriorElem(Node *pCurrentNode, Node *pPreNode);//获取指定元素的前驱 bool NextElem(Node *pCurrentNode, Node *pNextNode);//获取指定元素的后继 void ListTraverse();								 //遍历线性表 bool ListInsert(int i, Node *pNode);				 //在第i个位置插入元素 bool ListDelete(int i, Node *pNode);				  //删除第i个位置的元素 bool ListInsertHead(Node *pNode);bool ListInsertTail(Node *pNode);
private:Node *m_pList;int  m_iLength;//当前长度 
};
#endif
List.cpp

#include<iostream>
#include"List.h"
using namespace std;List::List()
{m_pList = new Node;m_pList->data = 0;m_pList->next = NULL;m_iLength = 0;
}
List::~List()
{ClearList();delete m_pList;m_pList = NULL;
}
void List::ClearList()
{Node *currentNode = m_pList->next;while (currentNode != NULL){Node *temp = currentNode->next;delete currentNode;currentNode = temp;}m_pList->next = NULL;m_iLength = 0;
}
bool List::ListEmpty()
{if (0 == m_iLength)return true;elsereturn false;}
int List::ListLength()
{return m_iLength;
}
bool List::GetElem(int i, Node *pNode)
{if (i<0 || i >= m_iLength)return false;Node *currentNode = m_pList;for (int k = 0; k <= i; k++){currentNode = currentNode->next;}pNode->data = currentNode->data;return true;
}
int List::LocateElem(Node *pNode)
{Node *currentNode = m_pList;int count = 0;while (currentNode->next != NULL){currentNode = currentNode->next;if (currentNode->data == pNode->data){return count;           //小细节:当有重复时,只会返回第一次出现的 }count++;//为什么放后面?小细节:头节点数据域无意义,0是我们找到的头节点后的第一个节点 }return -1;
}bool List::PriorElem(Node *pCurrentNode, Node *pPreNode)
{Node *currentNode = m_pList;Node *currentNodeBefore = NULL;while (currentNode->next != NULL){currentNodeBefore = currentNode;currentNode = currentNode->next;if (currentNode->data == pCurrentNode->data){if (currentNodeBefore == m_pList)return false;pPreNode->data = currentNodeBefore->data;return true;}}return false;}
bool List::NextElem(Node *pCurrentNode, Node *pNextNode)
{Node *currentNode = m_pList;while (currentNode->next != NULL){currentNode = currentNode->next;if (currentNode->data == pCurrentNode->data){if (currentNode->next == NULL)return false;pNextNode->data = currentNode->next->data;return true;}}return false;
}void List::ListTraverse()
{Node *currentNode = m_pList;while (currentNode->next != NULL){currentNode = currentNode->next;currentNode->printNode();}cout << "m_iLength = " << m_iLength << endl;
}bool List::ListInsertHead(Node *pNode)
{Node *temp = m_pList->next;  //头节点指向下一个结点的地址赋给temp保存起来Node *newNode = new Node;if (newNode == NULL)        //如果申请内存失败return false;newNode->data = pNode->data;//数据域先赋给新结点m_pList->next = newNode;     //头结点与新结点连接newNode->next = temp;       //新结点与后面结点连接的m_iLength++;                //插入成功长度加1return true;}bool List::ListInsertTail(Node *pNode)
{Node *currentNode = m_pList;while (currentNode->next != NULL)   //先找到尾结点{currentNode = currentNode->next;}Node *newNode = new Node;if (newNode == NULL)            //申请内存是否成功return  false;newNode->data = pNode->data;   //数据域先赋给新结点newNode->next = NULL;           //插入后充当尾部currentNode->next = newNode;  // 插入前的尾部与新结点连接m_iLength++;           //插入成功长度加1return true;
}bool List::ListInsert(int i, Node *pNode)
{if (i<0 || i>m_iLength)    //插入位置合理性判断return false;Node *currentNode = m_pList;for (int k = 0; k<i; k++)          //找到要插入的位置{currentNode = currentNode->next;}Node *newNode = new Node;      //申请新结点if (newNode == NULL)          //申请是否成功return  false;newNode->data = pNode->data;  //数据域传入newNode->next = currentNode->next;//当前结点所指向的下一结点的地址传给新结点currentNode->next = newNode;//当前结点与新结点连接m_iLength++;              //插入成功长度加1return true;
}bool List::ListDelete(int i, Node *pNode)
{if (i<0 || i >= m_iLength)    //删除合理性判断return false;Node *currentNode = m_pList;Node *currentNodeBefore = NULL;//当前结点前一结点for (int k = 0; k <= i; k++)     //找到删除位置和前一结点{currentNodeBefore = currentNode;currentNode = currentNode->next;}currentNodeBefore->next = currentNode->next;//当前结点的前一结点与其后一结点直接相连,相当于删除当前结点pNode->data = currentNode->data;//删除数据传出delete currentNode;      //释放内存,已经没用了currentNode = NULL;       //为了安全指为NULLm_iLength--;            //删除成功长度-1return true;
}

main.cpp

#include<iostream>
#include"List.h" 
using namespace std;int main(void)
{Node node1;node1.data = 3;Node node2;node2.data = 4;Node node3;node3.data = 5;Node node4;node4.data = 6;Node node5;node5.data = 77;List *pList = new List();cout << "从头部插入:" << endl;pList->ListInsertHead(&node1);pList->ListInsertHead(&node2);pList->ListInsertHead(&node3);pList->ListInsertHead(&node4);pList->ListTraverse();cout << "清除~~~";pList->ClearList();cout << "清除~~~DONE" << endl;cout << "从尾部插入:" << endl;pList->ListInsertTail(&node1);pList->ListInsertTail(&node2);pList->ListInsertTail(&node3);pList->ListInsertTail(&node4);pList->ListTraverse();cout << "从位置2插入 :" << endl;pList->ListInsert(2, &node5);pList->ListTraverse();cout << "从位置3删除 :" << endl;Node temp;pList->ListDelete(3, &temp);pList->ListTraverse();cout << "从位置1取出放入temp" << endl;pList->GetElem(1, &temp);cout << "temp = " << temp.data << endl;pList->PriorElem(&node2, &temp);cout << "从位置1取前驱temp" << endl;cout << "temp = " << temp.data << endl;pList->NextElem(&node2, &temp);cout << "从位置1取后继temp" << endl;cout << "temp = " << temp.data << endl;cout << "isEmpte:" << boolalpha << pList->ListEmpty() << endl;delete pList;pList = NULL;return 0;
}




这篇关于数据冒险之单链表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/542618

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元