数据冒险之顺序表应用

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

相关文章

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

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

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

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

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

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

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

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

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

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