#include<iostream>
using namespace std;
template<typename T>
class SqList
{
private:
int count;//实际元素个数
int Maxsize;//数组最大长度
T *elem;
public:
SqList(int size);//构造函数
~SqList();//析构函数
int Length(){
return count;
};
void Init(int size);// 初始化
void Clear();
void Travers();//遍历
bool Full();
bool Empty();
bool GetElem(int position, T t);//获取指定位置元素
bool SetElem(int position, T t);
bool Insert(int position, const T &t);插入元素
bool Delete(int position, T t);
SqList(const SqList ©);//拷贝构造函数
};
template<typename T>
SqList<T>::SqList(int size)
{
Maxsize = size;
elem = new T[Maxsize];
count = 0;
}
template<typename T>
SqList<T>::SqList(const SqList ©)
{
count = copy.Length();
if (elem != NULL) delete[]elem;
Maxsize = copy.Maxsize;
elem = new T[Maxsize];
for (int i = 0; i < count; i++)
elem[i] = copy.elem[i];
}
template<typename T>
SqList<T>::~SqList()
{
delete[]elem;
}
template<typename T>
void SqList<T>::Init(int size)
{
Maxsize = size;
if (elem != NULL) delete[] elem;
elem = new T [Maxsize];
count= 0;
}
template<typename T>
void SqList<T>::Clear()
{
count = 0;
}
template<typename T>
void SqList<T>::Travers()
{
for (int i = 0; i < Length(); i++)
cout << elem[i];
cout << endl;
}
template<typename T>
bool SqList<T>::Full()
{
return count == Maxsize;
}
template<typename T>
bool SqList<T>::Empty()
{
return count == Maxsize;
}
template<typename T>
bool SqList<T>::GetElem(int position, T t)
{
if (position<1 || position > Length())
return false;
else
{
t = elem[position - 1];
return true;
}
}
template<typename T>
bool SqList<T>::SetElem(int position, T t)
{
if (position <1 || position > Maxsize)
return false;
else
{
elem[position - 1]=t;
return true;
}
}
template<typename T>
bool SqList<T>::Insert(int position,const T &t)
{
if ( Full() || position <1 || position >Maxsize)
return false;
else
{
for (int i = Length() - 1; i >= position-1;i--)
elem[i+1] =elem[i];
elem[position - 1] = t;
count++;
return true;
}
}
template<typename T>
bool SqList<T>::Delete(int position, T t)
{
if (position <1 || position > Length())
return false;
else
{
t = elem[position - 1];
for (int i = position- 1; i <= Length()- 1; i++)
elem[i] = elem[i+1];
count--;
return true;
}
}
#include"SqList.h"
using namespace std;
int main()
{
SqList<int> sq(5);
cout << sq.Length();
cout << sq.Empty();
cout << sq.Full();
sq.Insert(1, 1);
SqList<int> sqt = sq;
cout << sqt.Length();
sqt.Travers();
/*cout<<sq.Insert(1, 1);
cout<<sq.Insert(2, 1);
cout << sq.Insert(1, 1);
cout << sq.Insert(2, 1);
cout << sq.Insert(1, 1);
cout << sq.Insert(2, 1);
cout << sq.Insert(2, 1);
cout << sq.Insert(1, 1);
cout << sq.Insert(2, 1);*/
//sq.Travers();
cout << sq.Length();
cin.get();
return 0;
}