#include<iostream>
using namespace std;
class Coordinate
{
friend ostream &operator<<(ostream &out,Coordinate &coor);
public:
Coordinate(int x=0,int y=0);
void printfCoordinate();
bool operator==(Coordinate &coor);
private:
int m_iX;
int m_iY;
};
Coordinate::Coordinate(int x,int y)
{
m_iX=x;
m_iY=y;
}
void Coordinate::printfCoordinate()
{
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;
}
class List
{
public:
List(int size);//创建
~List();//销毁
void ClearList();//清空线性表
bool ListEmpty();//判断是否为空表
int ListLenght();//求线性表的长度
bool GetElem(int i,Coordinate *e);//
int LocateElem(Coordinate *e);//
bool PriorElem(Coordinate *currentElem,Coordinate *preElem);//访问指定元素前驱
bool NextElem(Coordinate *currentElem,Coordinate *nextElem);//访问指定元素后继
bool ListInsert(int i,Coordinate *e);//在第i个位置插入元素
bool ListDelete(int i,Coordinate *e);//删除第i个位置的元素
void ListTraverse();//遍历线性表
private:
Coordinate *m_pList;
int m_iSize;//线性表大小
int m_iLength;//线性表长度
};
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::ListLenght()
{
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->printfCoordinate();
}
}
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;
}
int main()
{
Coordinate e1(3,5);
Coordinate e2(2,7);
Coordinate e3(6,1);
Coordinate temp(0,0);
List *list1= new List(10);
list1->ListInsert(0,&e1);
list1->ListInsert(1,&e2);
list1->ListInsert(2,&e3);
list1->ListTraverse();
list1->ListInsert(0,&temp);
list1->ListTraverse();
}