//顺序表#include using namespace std;template 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 SeqList ::SeqList() :_capacity(3) ,_data(new T[_capacity]) ,_size(0){}template SeqList ::~SeqList(){ delete[]_data;}template SeqList ::SeqList(const SeqList& s)//浅拷贝的问题-析构两次 :_capacity(s._capacity) ,_data(s._data) ,_size(s._size){}template SeqList & SeqList ::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 void SeqList ::_CheakCapacity(){ if(_size>=_capacity) { _capacity=_capacity*2+3; T* tmp=new T[_capacity]; memcpy(tmp,_data,sizeof(T)* _size); free(_data); _data=tmp; }}template void SeqList ::PushBack(const T &x){ _CheakCapacity(); _data[_size++]=x;}template void SeqList ::PopBack(){ if(_size!=0) { _size--; }}template void SeqList ::Print(){ size_t i; for(i=0;i<_size;i++) { printf("%d ",_data[i]); } printf("\n");}void Test1(){ SeqList sl1; SeqList sl2; sl1.PushBack(1); sl1.PushBack(2); sl1.PushBack(3); sl1.Print(); //SeqList sl2(sl1); sl2=sl1;//涉及深浅拷贝 sl2.Print(); }void Test2(){ SeqList 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 using namespace std;template struct ListNode{ T _data; ListNode* _next; ListNode(const T &x) :_data(x) ,_next(NULL) {}};template class List{public: List(); ~List(); List(const List& l);// List & operator=(const List& l); void _Clear(); void PushBack(const T& x); void PopBack(); void Print();private: ListNode * _head; ListNode * _tail;};template List ::List() :_head(NULL) ,_tail(NULL){}template List ::~List(){ _Clear();}template void List ::_Clear(){ ListNode * cur=_head; while(cur) { ListNode * del=cur; cur=cur->_next; delete del; } _head=NULL;//易忘 _tail=NULL;}template //有问题List & List ::operator=(const List& l){ if(this!=&l) { ListNode *l._head=new ListNode * head; l._head->_data=_data; l._head->_next=_next; } return *this;}template void List ::PushBack(const T& x){ if(_head==NULL) { _head=new ListNode (x); _tail=_head; } else { _tail->_next=new ListNode (x); _tail=_tail->_next; }}template void List ::PopBack(){ if(_head==_tail) { delete _head; _head=NULL;//易忘 _tail=NULL; } else { ListNode * cur=_head; while(cur->_next!=_tail) { cur=cur->_next; } cur->_next=NULL; delete _tail; _tail=cur; }}template void List ::Print(){ ListNode *cur=_head; while(cur) { cout< _data<<"->"; cur=cur->_next; } cout<<"Tail"<
l1; List
l2=l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.Print(); l2.Print();}void Test2(){ List
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;}