模板顺序链表

对于模板这个东西,我感觉好像概念清楚,但一直没机会动手写一写。今天终于动手了,写了才知道自己还是有很多相关的东西不知道的。今天写了一个模板顺序链表,还花了不少时间,以后有机会将会写更多的模板数据结构。下面的数据结构支持内存自动增长。有查找,插入,删除,赋值等简单基本操作

#ifndef AFX_XTLIST_H__ 
#define AFX_XTLIST_H__ 

#if _MSC_VER > 1000
#pragma once
#endif

#include <algorithm>

//顺序链表
#define ADD_SIZE 10
#define E_ERRO   -1
template <class T>
class XTList  
{
public:
XTList(int nCount = ADD_SIZE);

virtual ~XTList();
XTList(const XTList<T> &src);
T &operator [](int);
XTList &operator = (const XTList<T> &src);
public:
//插入数据到尾端
bool   InsertBack(const T &data);
//查找元素 T必须支持== 操作
int    FindData(const T &data);
//删除元素
bool   RemoveData(const T &data);
bool   RemoveData(int nIndex);
void   RemoveAll();
//得到元素个数
int    GetCurDataCount() const;
//得到分配空间大小
int    GetMaxSize() const;
//得到元素
bool   GetData(int nIndex,T &data) const;
//判断是否为空
bool   IsEmpty();
private:
//复制数据
void   copy(const XTList<T> &src);
private:
T     *m_pData;  //模板数据
int    m_nMaxSize;  //数据最多个数
int    m_nCurIndex;//当前数据个数
};

//以上是类声明
///
template <class T>
XTList<T>::XTList(int nCount)
{
m_nCurIndex = 0;
m_nMaxSize = nCount;
m_pData = new T[nCount];
}

template <class T>
XTList<T>::XTList(const XTList<T> &src)
{
m_nMaxSize = src.GetMaxSize();
m_pData = new T[m_nMaxSize];
copy(src);
}

template <class T>
XTList<T>::~XTList()
{
delete []m_pData;
}

template <class T>
T &XTList<T>::operator [](int nIndex)
{
if (nIndex > E_ERRO && nIndex < m_nCurIndex)
return m_pData[nIndex];
else
return m_pData[0];
}

template <class T>
XTList<T> &XTList<T>::operator = (const XTList<T> &src)
{
m_nMaxSize = src.GetMaxSize();
m_pData = new T[m_nMaxSize];
copy(src);

return *this;
}

//在末尾插入数据
template <class T>
bool XTList<T>::InsertBack(const T &data)
{
bool bTrue = false;
if (m_nCurIndex < m_nMaxSize - 1)
{
m_pData[m_nCurIndex] = data;
m_nCurIndex ++;

bTrue = true;
}
else if (m_nCurIndex == m_nMaxSize - 1)
{
//空间不够,继续分配空间
m_nMaxSize += ADD_SIZE;
T *pData = new T[m_nMaxSize];
std::copy(m_pData,m_pData+(m_nMaxSize - ADD_SIZE),pData);
delete []m_pData;

m_pData = pData;
m_pData[m_nCurIndex] = data;
m_nCurIndex ++;
bTrue = true;
}
else
{
bTrue = false;
}

return bTrue;
}

//查找元素
template <class T>
int XTList<T>::FindData(const T &data)
{
int nIndex = E_ERRO;
for (int i = 0; i < m_nCurIndex; ++i)
{
if (m_pData[i] == data)
nIndex = i;
}

return nIndex;
}

//删除元素
template <class T>
bool XTList<T>::RemoveData(const T &data)
{
int nIndex = FindData(data);

return RemoveData(nIndex);
}

//删除元素
template <class T>
bool XTList<T>::RemoveData(int nIndex)
{
if (nIndex > E_ERRO)
{
for (int i = nIndex; i < m_nCurIndex - 1; ++i)
{
m_pData[i] = m_pData[i+1];
}

m_nCurIndex --;
return true;
}
else
{
return false;
}
}

//得到元素个数
template <class T>
int XTList<T>::GetCurDataCount() const
{
return m_nCurIndex;
}

//得到分配空间大小
template <class T>
int XTList<T>::GetMaxSize() const
{
return m_nMaxSize;
}

//得到元素
template <class T>
bool XTList<T>::GetData(int nIndex,T &data) const
{
if (nIndex > E_ERRO && nIndex < m_nCurIndex)
{
data = m_pData[nIndex];
return true;
}
else
{
return false;
}
}

//复制数据
template <class T>
void XTList<T>::copy(const XTList<T> &src)
{
m_nCurIndex = 0;
T data;
for (int i = 0; i < src.GetCurDataCount(); ++i)
{
src.GetData(i,data);
InsertBack(data);
}
}

//判断是否为空
template <class T>
bool XTList<T>::IsEmpty()
{
if (m_nCurIndex > 0)
return true;
else
return false;
}

template <class T>
void XTList<T>::RemoveAll()
{
m_nCurIndex = 0;
}
#endif // !defined(AFX_XTLIST_H__581AC07C_E9AD_4228_836F_67A84100455D__INCLUDED_)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值