#ifndef LIST_H
#define LIST_H
#include"Coordinate.h"
class List
{
public:
List(int size);
~List();
void CleanList();
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); //获取指定元素的后继
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;
};
#endif#include"List.h"
#include
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;
}
//直接将m_iLength设为0,此时list中的数据还是存在的,但是已经被忽略了,
//再有数据的时候直接插入将其覆盖掉就可以了。
void List::CleanList()
{
m_iLength=0;
}
bool List::ListEmpty()
{
if(m_iLength==0)
{
return true;
}
return 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)
{
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
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#include"Coordinate.h"
#include
using namespace std;
Coordinate::Coordinate(int x,int y)
{
m_iX=x;
m_iY=y;
}
void Coordinate::printCoordinate()
{
cout<<"("<
<<","<
<<")"<
m_iX==coor.m_iX&&this->m_iY==coor.m_iY) { return true; } return false; }#include
#include
#include"List.h" #include
#include"Coordinate.h" using namespace std; /****************************************************************************************/ /**线性表--顺序表**/ // 3 5 6 //其中假设指定元素是5,那么前驱就是3,后继就是6。 //bool InitList(List **list); //创建线性表 //void DestroyList(List *list); //销毁线性表 //void CleanList(List *list); //清空线性表 //bool ListEmpty(List *list); //判断线性表是否为空 //int ListLength(List *list); //获取线性表长度 //bool getElem(List *list,int i,Elem *e); //获取指定元素 //int locateElem(List *list,Elem *e); //寻找第一个满足e的数据元素的位序 //bool PriorElem(List *list,Elem *currentElem,Elem *preElem); //获取指定元素的前驱 //bool NextElem(List *list,Elem *currentElem,Elem *nextElem); //获取指定元素的后继 //bool ListInsert(List *list,int i,Elem *e); //在第i个位置插入元素 //bool ListDelete(List *list,int i,Elem *e); //删除第i个位置的元素 //void ListTraverse(List *list); //遍历线性表 /****************************************************************************************/ int main(void) { //3 5 7 2 9 1 8 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(); list1->~List(); system("pause"); return 0; }
以上内容来源于慕课网。