/*
功能:编写一个名为List的类模板
作者:WindCoder
日期:2013-12-12
*/
#include <iostream>
using namespace std;
template <class T>
class List
{
public:
List();
~List();
bool Add(T a);
void Clear();
void Display ();
bool Del(int num);
bool Addone(T a,int pos);
bool Find(T a);
private:
int aSize;
T *m_aData;
};
//构造函数
template <class T>
List<T>::List()
{
aSize = 0;
m_aData=NULL;
}
//析构函数
template <class T>
List<T>::~List()
{
delete m_aData;
}
/************************************************************************
函数名:Add(T a)
功能:插入元素至末尾
参数:a 要插入的元素
返回值:true成功,false失败
************************************************************************/
template <class T>
bool List<T>::Add(T a)
{
T *p;
p= new T[aSize+1];
if (!p)
{
return false;
}
for (int i=0;i<aSize;i++)
{
p[i]=m_aData[i];
}
p[aSize] = a;
delete m_aData;
m_aData = p;
aSize++;
return true;
}
/************************************************************************
函数名:Clear();
功能:清空
参数:无
返回值:无
************************************************************************/
template <class T>
void List<T>::Clear()
{
delete m_aData;
m_aData = NULL;
aSize = 0;
}
/************************************************************************
函数名:Display ()
功能:遍历
参数:无
返回值:无
************************************************************************/
template <class T>
void List<T>::Display ()
{
for(int i=0;i<aSize;i++)
{
cout<<m_aData[i]<<" ";
}
}
/************************************************************************
函数名:Del(int pos)
功能:删除
参数:int pos 要删除的元素所在位置(从0开始)
返回值:true成功,false失败
************************************************************************/
template <class T>
bool List<T>::Del(int pos)
{
int i=0;
if (pos<0||pos>=aSize)
{
return false;
}
T *p;
p = new T[aSize-1];
for (;i<pos;i++)
{
p[i] = m_aData[i];
}
for(i=pos+1;i<aSize;i++)
{
p[i-1]=m_aData[i];
}
delete m_aData;
m_aData = p;
aSize--;
return true;
}
/************************************************************************
函数名:Addone(T a,int pos);
功能:在特定位置插入元素
参数:T a 要插入的值
int pos 要插入的位置
返回值:true成功,false失败
************************************************************************/
template <class T>
bool List<T>::Addone(T a,int pos)
{
int i=0;
if (pos<0||pos>aSize)
{
return false;
}
T *p;
p = new T[aSize+1];
aSize++;
for (;i<pos;i++)
{
p[i] = m_aData[i];
}
p[pos]=a;
for(i=pos+1;i<aSize;i++)
{
p[i+1]=m_aData[i];
}
delete m_aData;
m_aData = p;
return true;
}
/************************************************************************
函数名:Find(T a);
功能:查找指定元素
参数:T a 待查找元素
返回值:true存在,false不存在
************************************************************************/
template <class T>
bool List<T>:: Find(T a)
{
for (int i=0;i<aSize;i++)
{
if (a==m_aData[i])
{
return true;
}
}
return false;
}
int main(void)
{
int i;
List<int>list;
list.Add(1);
list.Add(5);
list.Add(13);
list.Display();
cout<<endl;
list.Del(0);
list.Display();
cout<<endl;
list.Addone(8,2);
list.Display();
cout<<endl;
i=list.Find(4);
if (i)
{
cout<<"元素存在"<<endl;
}
else
{
cout<<"元素不存在"<<endl;
}
system("pause");
return 0;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有