数据冒险之单链表

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

相关文章

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内