数据冒险之单链表

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

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

烟火目标检测数据集 7800张 烟火检测 带标注 voc yolo

一个包含7800张带标注图像的数据集,专门用于烟火目标检测,是一个非常有价值的资源,尤其对于那些致力于公共安全、事件管理和烟花表演监控等领域的人士而言。下面是对此数据集的一个详细介绍: 数据集名称:烟火目标检测数据集 数据集规模: 图片数量:7800张类别:主要包含烟火类目标,可能还包括其他相关类别,如烟火发射装置、背景等。格式:图像文件通常为JPEG或PNG格式;标注文件可能为X

pandas数据过滤

Pandas 数据过滤方法 Pandas 提供了多种方法来过滤数据,可以根据不同的条件进行筛选。以下是一些常见的 Pandas 数据过滤方法,结合实例进行讲解,希望能帮你快速理解。 1. 基于条件筛选行 可以使用布尔索引来根据条件过滤行。 import pandas as pd# 创建示例数据data = {'Name': ['Alice', 'Bob', 'Charlie', 'Dav

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者