#include<iostream>
#include<string.h>
using namespace std;
template<class T>
class SeqList{
public:
SeqList();
SeqList(const SeqList<T>& seq);
SeqList& operator=(const SeqList<T>& seq);
~SeqList();
void PushBack(const T& d);
void PushFront(const T& d);
void PopBack();
void PopFront();
void Display();
private:
void CheckCapacity(int n=0);
private:
int _size;
int _capacity;
T* _pdata;
};
template<class T>
SeqList<T>::SeqList()
:_size(0)
,_capacity()
,_pdata(NULL)
{}
template<class T>
SeqList<T>::SeqList(const SeqList<T>& seq){
_pdata=seq._capacity;
for(int i=0;i<seq._size;i++){
_pdata[i]=seq._pdata[i];
}
_size=seq._size;
_capacity=seq._capacity;
}
template<class T>
SeqList<T>& SeqList<T>:: operator=(const SeqList<T>& seq){
swap(_pdata,seq._pdata);
_size=seq._size;
_capacity=seq._capacity;
return *this;
}
template<class T>
SeqList<T>::~SeqList(){
if(_pdata!=NULL){
delete[] _pdata;
_pdata=NULL;
_size=0;
_capacity=0;
}
}
template<class T>
void SeqList<T>::CheckCapacity(int n){
if(_size==_capacity||n>_capacity){
int newcapacity=2*_capacity+1;
if(n>_capacity){
newcapacity=n;
}
T* tmp=new T[newcapacity];
for(int i=0;i<_size;i++){
tmp[i]=_pdata[i];
}
delete[] _pdata;
_pdata=tmp;
_capacity=newcapacity;
}
}
template<class T>
void SeqList<T>::PushBack(const T& d){
CheckCapacity();
_pdata[_size++]=d;
}
template<class T>
void SeqList<T>::PushFront(const T& d){
CheckCapacity();
for(int i=_size;i>0;i--){
_pdata[i]=_pdata[i-1];
}
//memmove(_pdata+1,_pdata,sizeof(T)*_size);
_pdata[0]=d;
_size++;
}
template<class T>
void SeqList<T>::PopBack(){
if(_size==0){
return ;
}
_size--;
}
template<class T>
void SeqList<T>::PopFront(){
for(int i=0;i<_size-1;i++){
_pdata[i]=_pdata[i+1];
}
--_size;
}
template<class T>
void SeqList<T>::Display(){
for(int i=0;i<_size;i++){
cout<<_pdata[i]<<' ';
}
cout<<endl;
}
int main(){
SeqList<int> s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.PopBack();
s1.Display();
}