关于Coordinate坐标的代码在前面几次数据结构的博客中写过了,今天做了一些修改,主要是添加了一个==的运算符重载。
方法是类似,讲篇一中的int改成Coordinate, 然后重载运算符重新写一下,最后调试一些,应该没有问题
下面看具体代码
1、List.h
#ifndef LIST_H
#define LISH_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);
bool ListDelete(int i, Coordinate *e);
private:
Coordinate *m_pList;
int m_iSize;
int m_iLength; //当前线性表已经放入的元素个数
};
#endif
#include "List.h"
#include <iostream>
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(m_iLength == 0)
return true;
else
return false;
// return m_iLength == 0 ? true : false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem(int i, Coordinate *e)
{
if (i<0 || i>=m_iSize)
{
return false;
}
*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 (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem = m_pList[temp - 1];
return true;
}
}
}
bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == m_iLength - 1)
{
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;
// m_pList[i].printCoordinate();
}
}
bool List::ListInsert(int i, Coordinate *e)
{
if (i < 0 || 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++)
{
m_pList[k-1] = m_pList[k];
}
m_iLength--;
return true;
}
3、Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H
#include <iostream>
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 m_iX;
int m_iY;
};
#endif
4、Coordinate.cpp
#include "Coordinate.h"
#include <iostream>
using namespace std;
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
void Coordinate::printCoordinate()
{
cout << "(" << m_iX << "," << m_iY << ")" << endl;
}
ostream &operator<<(ostream &out, Coordinate &coor)
{
out << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
return out;
}
bool Coordinate::operator==(Coordinate &coor)
{
if (this->m_iX == coor.m_iX && this->m_iY == coor.m_iY)
{
return true;
}
return false;
}
5、测试代码
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "List.h"
int main(int argc, char** argv)
{
Coordinate e1(3, 5);
Coordinate e2(5, 7);
Coordinate e3(6, 8);
Coordinate temp(0, 0);
List *list1 = new List(10);
list1->ListInsert(0, &e1);
list1->ListInsert(1, &e2);
list1->ListInsert(2, &e3);
list1->ListTraverse();
delete list1;
list1 = NULL;
return 0;
}