实现一个线性表的顺序存储:
要求:初始化,在某个位置插入元素、删除元素、修改元素、查找元素、返回当前长度、清空整个线性表,并模板化。
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
#define OR_SUCCESS 0
#define OR_BUFFER_OVERFLOW 1
#define OR_WRONG_LOC 2
#define OR_NO_INIT 3
//函数的返回值只代表函数是否成功
//用函数参数传递错误信息
template<class DataType>
class COrderList
{
public:
COrderList();
~COrderList();
public:
bool InitList( //初始化动态线性表
_In_ int nMaxLent, //参数0:当前最大长度
_Out_ int &nError //参数1:用于得到一个错误码
);
bool ListInsert( //在某个位置插入一条数据
_In_ int nLoc, //参数0:要插入数据的位置
_In_ DataType nEle, //参数1:要插入元的元素
_Out_ int& nError //参数2:用于得到一个错误码
);
bool ListDelete( //在某个位置删除一个数据
_In_ int nLoc, //参数0:要删除的位置
_Out_ DataType& nEle, //参数1:穿出被删除的数据
_Out_ int& nError //参数2:用于得到一个错误码
);
bool ListChange( //在某个位置修改元素数据
_In_ int nLoc, //参数0:要修改的位置
_In_ DataType nEle, //参数1:要修改成的数值
_Out_ int& nError //参数2:用于得到一个错误码
);
bool LocateElem( //查找指定元素,输出位置
_In_ DataType nEle, //参数0:要查找的元素数据
_Out_ int &Loc, //参数1:返回元素位置
_Out_ int& nError //参数2:用于得到一个错误码
);
bool ListLenght( //求线性表的长度
_Out_ int& nLenth, //参数0:用于获得线性表长度
_Out_ int& nError //参数1:用于得到一个错误码
);
bool ClearList( //清空线性表
_Out_ int& nError //参数0:用于得到一个错误码
);
void PrintList()
{
for (int i = 0; i < m_nLenth; i++)
{
cout << m_pBuf[i] << " ";
}
cout << endl << endl;
}
private:
DataType* m_pBuf; //线性表缓冲区
int m_nLenth; //线性表长度
int m_nMaxLenth; //当前线性表的最大长度
};
template<class DataType>
COrderList<DataType>::COrderList() :m_nLenth(0)
{
}
template<class DataType>
COrderList<DataType>::~COrderList()
{
}
//************************************************************
// 函数名称: InitList
// 函数说明: 初始化动态线性表,给出大小
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _In_ int nMaxLent 初始化大小
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::InitList(
_In_ int nMaxLent,
_Out_ int &nError
)
{
//1.先判断缓冲区指针是否为空,若不为空先释放
if (m_pBuf != NULL)
{
delete[] m_pBuf;
}
//2.申请特定长度的空间
m_pBuf = new DataType[nMaxLent];
//3.给最大长度复制
m_nMaxLenth = nMaxLent;
//4.返回
nError = OR_SUCCESS;
return true;
}
//************************************************************
// 函数名称: ListInsert
// 函数说明: 向顺序表中插入一条数据
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _In_ int nLoc 要插入数据的位置
// 参 数: _In_ int nEle 要插入元的元素
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::ListInsert(_In_ int nLoc, _In_ DataType nEle, _Out_ int& nError)
{
//1.检查是否传递了一个错误位置
if (nLoc > m_nLenth)
{
nError = OR_WRONG_LOC;
return false;
}
//2.检查一下,当前长度是否已经和最大长度相等
//如果相等,就申请出来一片更大的区域,将原来的数据拷贝进去,再将原来的区域释放。
if (m_nLenth == m_nMaxLenth)
{
//2.1 申请一片更大的空间
DataType* pTemp = new DataType[2 * m_nMaxLenth];
m_nMaxLenth = 2 * m_nMaxLenth;
//2.2 将原来的数据拷贝到新申请的空间中
for (int i = 0; i < m_nLenth; i++)
{
pTemp[i] = m_pBuf[i];
}
//2.3 释放原来的空间
delete[] m_pBuf;
m_pBuf = pTemp;
pTemp = nullptr;
}
//3.假如插入的不是最后一个元素,需要移动数据
for (int i = m_nLenth - 1; i >= nLoc; i--)
{
m_pBuf[i + 1] = m_pBuf[i];
}
//4.在该插入数据的地方赋值
m_pBuf[nLoc] = nEle;
//5.线性表长度需要自增
m_nLenth++;
//6.返回
nError = OR_SUCCESS;
return true;
}
//************************************************************
// 函数名称: ListDelete
// 函数说明: 在某个位置删除一个数据
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _In_ int nLoc 要删除的位置
// 参 数: _Out_ int & nEle 传出被删除的数据
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::ListDelete(_In_ int nLoc, _Out_ DataType& nEle, _Out_ int& nError)
{
//1.检测指针是否为空
if (m_pBuf == NULL)
{
nError = OR_NO_INIT;
return false;
}
//2.检测一下位置是否正确
if (nLoc >= m_nLenth || nLoc < 0)
{
nError = OR_WRONG_LOC;
return false;
}
//3.保存要删除的数据
nEle = m_pBuf[nLoc];
//4.开始删除数据
for (int i = nLoc; i < m_nLenth - 1; i++)
{
m_pBuf[i] = m_pBuf[i + 1];
}
//5.长度自减
m_nLenth--;
//动态缩减链表长度
if (m_nLenth == m_nMaxLenth / 2)
{
//5.1申请一片更小的空间
DataType* pTemp = new DataType[m_nMaxLenth / 2];
m_nMaxLenth = m_nMaxLenth / 2;
//5.2将原来的数据拷贝到新的空间中
for (int i = 0; i < m_nLenth; i++)
{
pTemp[i] = m_pBuf[i];
}
delete[] m_pBuf;
m_pBuf = pTemp;
pTemp = nullptr;
}
//6.返回
nError = OR_SUCCESS;
return true;
}
//************************************************************
// 函数名称: ListChange
// 函数说明: 修改某一位置的元素数据
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _In_ int nLoc 要修改元素的位置
// 参 数: _In_ int nEle 要修改为的元素数据
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::ListChange(_In_ int nLoc, _In_ DataType nEle, _Out_ int& nError)
{
//1.检测指针是否为空
if (m_pBuf == NULL)
{
nError = OR_NO_INIT;
return false;
}
//2.检测一下位置是否正确
if (nLoc >= m_nLenth || nLoc < 0)
{
nError = OR_WRONG_LOC;
return false;
}
//3.修改元素数据
m_pBuf[nLoc] = nEle;
//4.返回
nError = OR_SUCCESS;
return true;
}
//************************************************************
// 函数名称: LocateElem
// 函数说明: 查找指定元素,输出位置
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _In_ int nEle 要查找的元素数据
// 参 数: _Out_ int &Loc 返回元素位置
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::LocateElem(_In_ DataType nEle, _Out_ int &Loc, _Out_ int& nError)
{
//1.先将Loc返回值置为-1,如果没有找到则传出此值,代表没有找到
Loc = -1;
//2.判断缓冲区是否为空,为空不执行,填写错误码
if (m_nLenth == 0)
{
nError = OR_BUFFER_OVERFLOW;
return false;
}
//3.在线性表中查找目标元素,传出匹配到的第一个位置
for (int i = 0; i < m_nLenth; i++)
{
if (m_pBuf[i] == nEle)
{
Loc = i;
nError = OR_SUCCESS;
return true;
}
}
//4.如果查找不成功,返回
nError = OR_WRONG_LOC;
return false;
}
//************************************************************
// 函数名称: ListLenght
// 函数说明:
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _Out_ int & nLenth 用于获得线性表长度
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::ListLenght(_Out_ int& nLenth, _Out_ int& nError)
{
//1.传出元素个数
nLenth = m_nLenth;
//2.返回
nError = OR_SUCCESS;
return true;
}
//************************************************************
// 函数名称: ClearList
// 函数说明: 清空线性表
// 作 者: Dylan
// 时 间: 2015/08/24
// 返 回 值: bool
// 参 数: _Out_ int & nError 用于得到一个错误码
//************************************************************
template<class DataType>
bool COrderList<DataType>::ClearList(_Out_ int& nError)
{
//1 将数组中数据清空
for (int i = 0; i < m_nLenth; i++)
{
m_pBuf[i] = 0;
}
//2.将长度置为0
m_nLenth = 0;
m_nMaxLenth = 0;
//3.返回
nError = OR_SUCCESS;
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
int nError;
COrderList<char> obj;
//插入元素
obj.InitList(2, nError);
obj.ListInsert(0, 'a', nError);
obj.ListInsert(1, 'b', nError);
obj.ListInsert(0, 'c', nError);
obj.ListInsert(1, 'd', nError);
obj.PrintList();
//删除元素
char EleDel;
obj.ListDelete(2, EleDel, nError);
obj.PrintList();
//修改元素
obj.ListChange(2, 'x', nError);
obj.PrintList();
//查找元素
int LocEle;
obj.LocateElem('x', LocEle, nError);
if (LocEle == -1)
{
cout << "查找失败" << endl << endl;
}
else
{
cout << "查找位置为:" << LocEle << endl << endl;
}
//返回当前长度
int nLength;
obj.ListLenght(nLength, nError);
cout << "线性表长度为:" << nLength << endl << endl;
//清空线性表并输出
obj.ClearList(nError);
obj.PrintList();
system("pause");
return 0;
}