#pragma once
template <typename T>
class SeqList
{
public:
SeqList(int capcity);
~SeqList();
int getLength();
int getCapacity();
int insert(T &t, int pos);
intget(int pos,T& t);
int Delete(int pos, T& t);
private:
int length;
int capacity;
T *pArray;
};
实现文件
#include "SeqList.h"
template <typename T>
SeqList<T>::SeqList(int capcity)
{
pArray = new T[capcity];
this->capacity = capcity;
this->length = 0;
}
template <typename T>
SeqList<T>::~SeqList()
{
delete[]pArray;
pArray = NULL;
length = 0;
capacity = 0;
}
template <typename T>
int SeqList<T>::getLength()
{
returnlength;
}
template <typename T>
int SeqList<T>::getCapacity()
{
return capacity;
}
template <typename T>
int SeqList<T>::insert(T &t, intpos)
{
int i = 0;
if ( pos < 0)
{
return -1;
}
for (i = length; i> pos; i--)
{
pArray[i] = pArray[i - 1];
}
pArray[i] = t;
length++;
return0;
}
template <typename T>
int SeqList<T>::get(intpos, T& t)
{
if ( pos < 0)
{
return -1;
}
t = pArray[pos];
return0;
}
template <typename T>
int SeqList<T>::Delete(intpos, T& t)
{
int i = 0;
if ( pos < 0)
{
return -1;
}
t = pArray[pos];
for (i = pos + 1; i <length;i++)
{
pArray[i - 1] = pArray[i];
}
length--;
return0;
}
头文件#pragma oncetemplate <typename T>class SeqList{public: SeqList(int capcity); ~SeqList(); int getLength(); int getCapacity(); int insert(T &t, int pos); int get(int pos,T& t);