数据冒险之单链表

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

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

Python实现将MySQL中所有表的数据都导出为CSV文件并压缩

《Python实现将MySQL中所有表的数据都导出为CSV文件并压缩》这篇文章主要为大家详细介绍了如何使用Python将MySQL数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到... python将mysql数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到另一个

SpringBoot整合jasypt实现重要数据加密

《SpringBoot整合jasypt实现重要数据加密》Jasypt是一个专注于简化Java加密操作的开源工具,:本文主要介绍详细介绍了如何使用jasypt实现重要数据加密,感兴趣的小伙伴可... 目录jasypt简介 jasypt的优点SpringBoot使用jasypt创建mapper接口配置文件加密

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

Oracle存储过程里操作BLOB的字节数据的办法

《Oracle存储过程里操作BLOB的字节数据的办法》该篇文章介绍了如何在Oracle存储过程中操作BLOB的字节数据,作者研究了如何获取BLOB的字节长度、如何使用DBMS_LOB包进行BLOB操作... 目录一、缘由二、办法2.1 基本操作2.2 DBMS_LOB包2.3 字节级操作与RAW数据类型2.