标签:c++ 模板的一个实现
/************************************ WZ ASUST 2016 模板写了一下,但发现不是模板 ************************************/ #include <iostream> using namespace std; #define OK 1 #define ERROR 0 template <class T> class Linklist { public: Linklist(int len=10,int max=20) :length(len) ,maxsize(max) { elem = new T[maxsize]; cout<<"linklist"<<endl; cout<<elem<<endl; cout<<elem+20<<endl; *(elem+20)=1; cout<<*(elem+20)<<endl; } int Getlength(Linklist < T > & L); int Getmaxsize(Linklist < T > & L); int DestroyList(Linklist < T > & L); ~Linklist() //销毁函数 { if(length==0);delete elem; cout<<"DestroyList"<<endl; }; int ClearList(Linklist < T > & L); void ShowList(Linklist < T > & L); void GetElem( int i , T e); /* bool ReturnElem( int i , T &e)//获得第i个元素,e必须是T类型的,例如int e { if(i<1||i>length) return false; if( e=elem[i]) return true; else return false; } */ T LocateElem(Linklist < T > & L , T e);//找到e元素的位置 T InsertElem(Linklist < T > & L , int i , T e);//插入 T paixu(); T reverse(); private: int length; int maxsize; int *elem; }; template<class T> int Linklist<T>::Getlength( Linklist &L) { cout<<L.length<<endl; } template<class T> int Linklist<T>::Getmaxsize( Linklist &L) { cout<<"maxsize is :"<<L.maxsize<<endl; } template<class T> int Linklist<T>::DestroyList(Linklist <T> &L) //销毁顺式表 { L.~Linklist(); } template <class T> int Linklist<T>::ClearList(Linklist < T > &L) //清空顺式表 { L.length = 0; if(L.length == 0) { cout<<"Clear OK"<<endl; return 1; } else { cout<<"Clear ERROR"<<endl; return 0; } } template <class T> void Linklist <T>::ShowList(Linklist <T> &L) { for(int i = 0 ; i<length ; i ++) cout<<L.elem[i]<<" "; cout<<endl; } template <class T> void Linklist <T>::GetElem( int i ,T e) {//just test *(elem+i)=e; length++; } template <class T> T Linklist <T>::LocateElem (Linklist <T> &L , T e) { for(int i = 0 ; i < L.length ; i ++) { if( L.elem[i] == e) { return i; break; } } return -1; } template <class T> T Linklist<T>::InsertElem(Linklist <T> &L , int i , T e) { if(i<1||i>L.length)return ERROR; for(int j = L.length ; j >i ; j--) { L.elem[j] = L.elem[j-1]; } L.elem[i-1] = e; L.length ++; return 1; } template <class T> T Linklist<T>::paixu() { int i=0,j=0,state=1; T t=0; for(i=0;i<length&&state;i++) { for(j=length;j>i;j--) if(elem[j]<elem[j-1]) {t=elem[j];elem[j]=elem[j-1];elem[j-1]=t;} } } template <class T> T Linklist<T>::reverse() { int i=0,j=length; T t=0; for(;i<j;i++,j--) { t= elem[i]; elem[i]=elem[j]; elem[j]=t; } } int main() { int e=10; Linklist<int> L; //L.initLinklist(L,20); L.ShowList(L); cout<<"WZZX"<<endl<<L.Getlength(L)<<"WZZX"<<endl; L.InsertElem(L,1,3); L.GetElem(1,3); L.ShowList(L); L.InsertElem(L,5,7); L.GetElem(6,8); L.ShowList(L); /// L.ReturnElem(1,&e); //error cout<<"----------------"<<endl; cout<<"1:"<<L.LocateElem(L,3)<<endl; //bug just once cout<<"e="<<e<<endl; cout<<"----------------"<<endl; cout<<"2:reverse"<<L.reverse()<<endl; L.ShowList(L); cout<<"----------------"<<endl; cout<<"3:paixu"<<L.paixu()<<endl; L.ShowList(L); cout<<"----------------"<<endl; cout<<"4:maxsize = "<<L.Getmaxsize(L)<<endl; // bug: what is the rand number? /* Linklist<char> Lc; Lc.InsertElem(Lc,1,‘A‘); Lc.GetElem(2,‘B‘); Lc.ShowList(Lc); */ } /******************************** WZ ASUST 2016 双向链表第一阶段 拷贝构造中内存泄露 *********************************/ #include"sts.h" template <class T> struct node { public:node(const T &d):next(NULL),prve(NULL),data(d){} T data; node<T> *next; node<T> *prve; }; template <class T> class dlist { private: node<T>* head;node<T>* tail; public: dlist():head(NULL),tail(NULL){}; ~dlist(){node<T>*cur=head;while(cur){node<T>*del=cur;cur=cur->next;delete del;}} void print(){node<T>*cur=head;while(cur){cout<<cur->data<< " ";cur=cur->next;}cout<<"ok"<<endl;} // void dlist(const dlist<T>&dl){}//while not error dlist & operator=(const dlist <T> & dl) { if(this!=&dl) { head=dl.head; tail=dl.tail; node<T>*dlcur=dl.head; node<T>*cur=dlcur; int xx=4; while(dlcur&&xx--) { cout<<"wzzx"<<xx<<endl; dlcur=dlcur->next; dlcur=cur; cur=cur->next; cout<<cur->data<<endl; } } } void pushback(const T &d) { if(head==NULL) { head=new node<T>(d); tail=head; } else { tail->next=new node<T>(d); tail->next->prve=tail; tail=tail->next; } } T popback() { if(head==NULL) { return 0; } else { return tail->data; } } void popfront(const T &d) { if(head==NULL) { head=new node<T>(d); tail=head; } else { head->prve=new node<T>(d); head->prve->next=head; head=head->prve; } } T popfront() { if(head==NULL) { return 0; } else { return head->data; } } }; void test() { dlist<int> int_dlist; cout<<"** 1 : ***************"<<endl; int_dlist.pushback(11); int_dlist.pushback(22); int_dlist.pushback(33); int_dlist.pushback(44); int_dlist.print(); cout<<"*** 2 :**************"<<endl; int_dlist.popfront(55); int_dlist.popfront(66); int_dlist.popfront(77); int_dlist.print(); cout<<"*** 3 :**************"<<endl; cout<<int_dlist.popfront()<<endl; cout<<int_dlist.popback()<<endl; cout<<"*** 4 :**************"<<endl; } int main() { dlist<int> int_dlist; cout<<"** 1 : ***************"<<endl; int_dlist.pushback(11); int_dlist.pushback(22); int_dlist.print(); dlist<int> int_dlist2; int_dlist2=int_dlist; int_dlist.print(); return 0; }
标签:c++ 模板的一个实现
原文地址:http://sts609.blog.51cto.com/11227442/1758669