//MyCTplate.h
namespace MyCla_Struct
{
template <class Type>
class CLN_List
{
public:
CLN_List();
CLN_List(int max); //构造函数,初始最大长度为max
CLN_List(const CLN_List & L);//复制构造函数
//void DestroyList(); //销毁线性表
void ClearList(); //清空线性表
bool ListEmpty(); //判断线性表是否为空 空true,否false
int Listlength(); //返回数据表中的元素个数
bool GetElem(int i,Type &e); //用e返回第i个元素
bool PriorElem(Type cur_e,Type &pre_e); //若cur_e是对象的元素且不是第一个元素,则用pre_e返回它的前驱失败返回false
bool NextElem(Type cur_e,Type &next_e); //若cur_e是对象的元素且不是最后一个,则用next_e返回它的后继失败返回flase
bool ListInsert(int i,Type e); //在i位置插入e,失败返回false
bool ListDelete(int i,Type &e);//删除i处的元素并且用e返回失败返回false
CLN_List operator =( CLN_List L); //重载=
~CLN_List();
protected:
int Max; //申请内存空间大小
int length;// 元素个数
Type *PL; //头指针
};
}
//CSK_Cla_Declare.h
#ifndef LIST
#define LIST
#include<new>
#include"MyCTplate.h"
using namespace MyCla_Struct;
template<class Type>
CLN_List<Type>::CLN_List()
{
Max = 0;
length = 0;
PL = new Type();
}
template<class Type>
CLN_List<Type>::CLN_List(int max)
{
PL = new Type[max];
length = 0;
Max = max;
}
template<class Type>
CLN_List<Type>::CLN_List(const CLN_List &L)
{
length = L.length;
Max = L.Max;
PL = new Type[Max];
for (int i = 0; i < length; i++)
L.PL[i] = L.PL[i];
}
template<class Type>
bool CLN_List<Type>::ListEmpty()
{
if (length == 0)
return true;
else
return false;
}
template<class Type>
void CLN_List<Type>::ClearList()
{
length = 0;
}
template<class Type>
int CLN_List<Type>::Listlength()
{
return length;
}
template <class Type>
bool CLN_List<Type>::GetElem(int i, Type &e)
{
if (i >= 0 && i < length)
{
e = PL[i];
return true;
}
else
{
return false;
}
}
template<class Type>
bool CLN_List<Type>::PriorElem(Type cur_e,Type &pri_e)
{
for (int i = 0; i < length;i++)
if (i>0&&cur_e == PL[i])
{
pri_e = PL[i - 1];
return true;
}
return false;
}
template<class Type>
bool CLN_List<Type>::NextElem(Type cur_e, Type &next_e)
{
for (int i = 0; i < length; i++)
if (i < length - 1&& cur_e == PL[i])
{
next_e = PL[i+1];
return true;
}
return false;
}
template<class Type>
bool CLN_List<Type>::ListInsert(int i, Type e)
{
if (i >= 0 && i <=length)
{
if (length >= Max) //lengh代表数据元素的个数,并且是下一个插入的指针
{
/*
new布局很坑,new布局不能代替realloc,具体为什么 我到现在也没高清,反正如果要用就要,重新申请,释放旧的,再重新指向指针。
*/
Max += 10;
Type *t = new Type[Max];
for(int i=0;i<length;i++)
t[i]=PL[i];
delete[]PL;
PL = t;
/*
Max += 10;
PL = new (PL)Type[Max]; */
}
int j = length;
for (; j > i; j--)
PL[j] = PL[j-1];
PL[j] = e;
length++;
return true;
}
return false;
}
template<class Type>
bool CLN_List<Type>::ListDelete(int i, Type &e)
{
if (i >= 0 && i < length)
{
e = PL[i];
int j;
for ( j = i; j < length-1; j++)
PL[j] = PL[j + 1];
length = j;
return true;
}
return false;
}
template<class Type>
CLN_List<Type> CLN_List<Type>::operator=(CLN_List p)
{
length = p.length;
Max = p.Max;
for (int i = 0; i < length; i++)
PL[i] = p.PL[i];
}
template<class Type>
CLN_List<Type>::~CLN_List()
{
delete []PL;
}
#endif
以后会不定期更新,自己没事儿写着玩,免不了有错误!请大家指教!##