#ifndef __MYTEMPLATE_H__ #define __MYTEMPLATE_H__ #pragma once template<class T> class List { public: List(); void Add(T&); void Remove(T&); int getNumber(); T* getWhich(int); //T* getMax() void Clear(); ~List(); protected: struct Node { Node * pNext; T * pT; }; Node * pFirst; int number; }; template<class T> List<T>::List() { pFirst=0; number=0; } template<class T> void List<T>::Add(T&t) { Node * temp=new Node; temp->pT=&t; temp->pNext=pFirst; pFirst=temp; number++; } template<class T> void List<T>::Remove(T&t) { Node * q=0; if(*(pFirst->pT)==t) { q=pFirst; pFirst=pFirst->pNext; } else { for(Node * p=pFirst;p->pNext;p=p->pNext) if(*(p->pNext->pT)==t) { q=p->pNext; p->pNext=q->pNext; break; } } if(q) { delete q->pT; delete q; number--; } } template<class T> int List<T>::getNumber() { return number; } template<class T> T* List<T>::getWhich(int id) { if(id>number) return 0; register unsigned int i=id; Node* p=pFirst; while(i>1) { p=p->pNext; i--; } return p->pT; } //template<class T> //T* List<T>::getMax() //{ // register unsigned int i=number; // Node* p=pFirst, pmax; // while(i>1) // { // p=p->pNext; // if (&(p->Pt) > &(pmax->Pt)) // pmax = p; // i--; // } // return pmax->pT; //} template<class T> void List<T>::Clear() { Node * p = pFirst; while(p)//=pFirst) { pFirst=pFirst->pNext; delete p->pT; delete p; p = pFirst; } pFirst=0; number=0; } template<class T> List<T>::~List() { Node * p = pFirst; while(p)// = pFirst) { pFirst=pFirst->pNext; delete p->pT; delete p; p = pFirst; } } #endif