//
#include<iostream>
using namespace std;
template<typename T>
class SeqList
{
public:
SeqList();
~SeqList();
SeqList(const SeqList& s);
SeqList& operator=(const SeqList &s);
void _CheakCapacity();
void PushBack(const T& x);
void PopBack();
void Print();
private:
size_t _capacity;
T* _data;
size_t _size;
};
template<typename T>
SeqList<T>::SeqList()
:_capacity(3)
,_data(new T[_capacity])
,_size(0)
{}
template<typename T>
SeqList<T>::~SeqList()
{
delete[]_data;
}
template<typename T>
SeqList<T>::SeqList(const SeqList& s)// -
:_capacity(s._capacity)
,_data(s._data)
,_size(s._size)
{}
template<typename T>
SeqList<T>& SeqList<T>::operator=(const SeqList &s)//
{
if(this!=&s)
{
_capacity=s._capacity;
_size=s._size;
delete _data;
_data=new T[_capacity];
memcpy(_data,s._data,_size);
}
return *this;
}
template<typename T>
void SeqList<T>::_CheakCapacity()
{
if(_size>=_capacity)
{
_capacity=_capacity*2+3;
T* tmp=new T[_capacity];
memcpy(tmp,_data,sizeof(T)* _size);
free(_data);
_data=tmp;
}
}
template<typename T>
void SeqList<T>::PushBack(const T &x)
{
_CheakCapacity();
_data[_size++]=x;
}
template<typename T>
void SeqList<T>::PopBack()
{
if(_size!=0)
{
_size--;
}
}
template<typename T>
void SeqList<T>::Print()
{
size_t i;
for(i=0;i<_size;i++)
{
printf("%d ",_data[i]);
}
printf("
");
}
void Test1()
{
SeqList<char> sl1;
SeqList<char> sl2;
sl1.PushBack(1);
sl1.PushBack(2);
sl1.PushBack(3);
sl1.Print();
//SeqList<char> sl2(sl1);
sl2=sl1;//
sl2.Print();
}
void Test2()
{
SeqList<char> sl1;//
sl1.PushBack(1);
sl1.PushBack(2);
sl1.PushBack(3);
sl1.PushBack(4);
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.Print();
}
int main()
{
Test1();
//Test2();
system("pause");
return 0;
}
//
#include<iostream>
using namespace std;
template<typename T>
struct ListNode
{
T _data;
ListNode* _next;
ListNode(const T &x)
:_data(x)
,_next(NULL)
{}
};
template<typename T>
class List
{
public:
List();
~List();
List(const List& l);//
List<T>& operator=(const List& l);
void _Clear();
void PushBack(const T& x);
void PopBack();
void Print();
private:
ListNode<T>* _head;
ListNode<T>* _tail;
};
template<typename T>
List<T>::List()
:_head(NULL)
,_tail(NULL)
{}
template<typename T>
List<T>::~List()
{
_Clear();
}
template<typename T>
void List<T>::_Clear()
{
ListNode<T>* cur=_head;
while(cur)
{
ListNode<T>* del=cur;
cur=cur->_next;
delete del;
}
_head=NULL;//
_tail=NULL;
}
template<typename T>//
List<T>& List<T>::operator=(const List& l)
{
if(this!=&l)
{
ListNode<T>*l._head=new ListNode<T>* head;
l._head->_data=_data;
l._head->_next=_next;
}
return *this;
}
template<typename T>
void List<T>::PushBack(const T& x)
{
if(_head==NULL)
{
_head=new ListNode<T>(x);
_tail=_head;
}
else
{
_tail->_next=new ListNode<T>(x);
_tail=_tail->_next;
}
}
template<typename T>
void List<T>::PopBack()
{
if(_head==_tail)
{
delete _head;
_head=NULL;//
_tail=NULL;
}
else
{
ListNode<T>* cur=_head;
while(cur->_next!=_tail)
{
cur=cur->_next;
}
cur->_next=NULL;
delete _tail;
_tail=cur;
}
}
template<typename T>
void List<T>::Print()
{
ListNode<T>*cur=_head;
while(cur)
{
cout<<cur->_data<<"->";
cur=cur->_next;
}
cout<<"Tail"<<endl;
}
void Test1()
{
List<int> l1;
List<int> l2=l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.Print();
l2.Print();
}
void Test2()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.Print();
l.PopBack();
l.PopBack();
l.PopBack();
l.Print();
}
int main()
{
Test1();
//Test2();
system("pause");
return 0;
}