数据冒险之顺序表应用
来源:互联网 发布:windows已阻止此软件 编辑:程序博客网 时间:2024/04/29 04:57
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;}
阅读全文
0 0
- 数据冒险之顺序表应用
- 数据冒险之顺序表
- 数据冒险之栈
- 数据冒险之单链表
- 数据冒险之栈应用(数制转换、括号匹配)
- 数据冒险之队列实例
- 数据冒险之二叉树(链表实现)
- 数据冒险
- 数据冒险之二叉树(数组)
- 数据冒险之单链表(简易通信录)
- 数据冒险之图以及最小生成树
- 顺序表筛选数据的应用
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- [BZOJ]3438: 小M的作物 最小割
- c++中的多线程
- github (mac)记录
- 在cygwin上面安装gcc
- Flask 学习笔记 --12
- 数据冒险之顺序表应用
- jQuery选择器——子元素过滤选择器
- Java跳出循环语句break、continue、return
- jQuery中将左右两个列表框的内容相互移动
- 应用服务器中对JDK的epoll空转bug的处理
- Activity生命周期
- MOOC清华《程序设计基础》第7章第1题:用结构体计算天数
- 字符串常量,指针,数组,[],*各关系梳理
- 团战可以输、提莫必须死