数据冒险之顺序表应用

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

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺