数据冒险之顺序表应用

2023-12-27 09:08
文章标签 数据 应用 顺序 冒险

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

List.h

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

List.cpp

#include<iostream>
#include"List.h"
using namespace std;List::List(int size)
{m_iSize = size;m_pList = new Coordinate[m_iSize];m_iLength = 0;
}
List::~List()    //将构造函数中的内存释放掉
{delete[]m_pList;m_pList = NULL;
}
void List::ClearList()   //将存在的元素清空,不等于清空内存
{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, Coordinate *e)
{if (i<0 || i >= m_iSize)return false;else*e = m_pList[i];return true;
}
int List::LocateElem(Coordinate *e)
{for (int i = 0; i<m_iLength; i++){if (m_pList[i] == *e)return i;}return -1;
}bool List::PriorElem(Coordinate *currentElem, Coordinate *preElem)    //前驱
{int  temp = LocateElem(currentElem);     //当前元素下标if (-1 == temp)    //当前元素不存在return false;else{if (0 == temp)       //当前元素为第一个元素,不存在前驱return false;else                  //当前元素存在前驱{*preElem = m_pList[temp - 1];return true;}}
}
bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{int  temp = LocateElem(currentElem);if (-1 == temp)                         //当前元素不存在return false;else{if ((m_iLength - 1) == temp)         //当前元素为最后一个元素,不存在后驱return false;else{*nextElem = m_pList[temp + 1];return true;}}
}void List::ListTraverse()
{for (int i = 0; i<m_iLength; i++){cout << m_pList[i] << endl;}
}bool List::ListInsert(int i, Coordinate *e)         //插入操作,先移动再插入
{if (i<0 || i>m_iLength)           //i=m_iLength时,在最后一位插入,不需要移动return false;for (int k = m_iLength - 1; k >= i; k--)    //从最后一个元素开始移动{m_pList[k + 1] = m_pList[k];}m_pList[i] = *e;m_iLength++;return true;
}
bool List::ListDelete(int i, Coordinate *e)       //删除操作,先删除再移动
{if (i<0 || i >= m_iLength) return false;*e = m_pList[i];for (int k = i + 1; k<m_iLength; k++)       //从第i+1个元素开始移动{m_pList[k - 1] = m_pList[k];}m_iLength--;return true;
}

coordinate.h

#ifndef COORDINATE_H
#define COORDINATE_H#include<ostream>
using namespace std;
class Coordinate
{friend ostream &operator<<(ostream &out,Coordinate &coor);
public:Coordinate(int x=0 ,int y=0);void printCoordinate();bool operator==(Coordinate &coor);
private:int x;int y;
};#endif

coordinate.cpp

#include"coordinate.h"
#include<iostream>
using namespace std;
Coordinate::Coordinate(int _x,int _y)
{x = _x;y = _y;
}
void Coordinate::printCoordinate()
{cout << "(" << x << "," << y << ")"<< endl;}//<<运算符重载,遍历操作时出现<<,所以要对其重载
ostream &operator<<(ostream &out, Coordinate &coor)
{cout << "(" << coor.x << "," << coor.y << ")" << endl;return out;
}
//==运算符重载   
/*
for (int i = 0; i<m_iLength; i++)
{
if (m_pList[i] == *e)    //这里有用到==,所以要对其重载
return i;
}*/
bool Coordinate::operator == (Coordinate &coor)
{if (this->x == coor.x&&this->y == coor.y)return true;elsereturn false;
}

main.cpp

#include <iostream>  
#include "List.h"  
#include"coordinate.h"
using namespace std;int main(void)
{List *List1 = new List(8);Coordinate e1(2, 3);Coordinate e2(1, 7);Coordinate e3(4, 3);Coordinate e4(6, 6);Coordinate e5(8, 2);Coordinate e6(9, 1);Coordinate e7(3, 3);//插入cout << "插入的坐标:" << endl;List1->ListInsert(0, &e1);List1->ListInsert(1, &e2);List1->ListInsert(2, &e3);List1->ListTraverse();cout << "已有坐标的个数:" << List1->ListLength() << endl;List1->ListInsert(3, &e4);List1->ListInsert(4, &e5);List1->ListInsert(5, &e6);List1->ListInsert(6, &e7);List1->ListTraverse();//删除cout << "删除的坐标:" << endl;Coordinate temp(0,0);List1->ListDelete(5, &temp);List1->ListTraverse();cout << "删除坐标为: " << temp << endl;delete List1;List1 = NULL;return 0;
}



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



http://www.chinasem.cn/article/542617

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

python 线程池顺序执行的方法实现

《python线程池顺序执行的方法实现》在Python中,线程池默认是并发执行任务的,但若需要实现任务的顺序执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录方案一:强制单线程(伪顺序执行)方案二:按提交顺序获取结果方案三:任务间依赖控制方案四:队列顺序消