//+------------------------------------------------------------------------- // // Microsoft Windows // // Copyright (c) 1998-1998 Microsoft Corporation // // File: tlist.h // //-------------------------------------------------------------------------- // // tlist.h --- template version of AList // #ifndef __TLIST_H__ #define __TLIST_H__ //#include "stdafx.h" //template //typedef BOOL (* TRelation) (T, T); // TListItem<> contains four more members than AListItem: one additional constructor, // a destructor, one member function, and one data member. template class TListItem { public: TListItem() { m_pNext=NULL; }; ~TListItem(); // new destructor static void Delete(TListItem* pFirst); // new deletion helper TListItem(const T& item) { m_Tinfo = item; m_pNext=NULL; }; // additional constructor. TListItem *GetNext() const {return m_pNext;}; void SetNext(TListItem *pNext) {m_pNext=pNext;}; LONG GetCount() const; TListItem* Cat(TListItem* pItem); TListItem* AddTail(TListItem* pItem) {return Cat(pItem);}; TListItem* Remove(TListItem* pItem); TListItem* GetPrev(TListItem *pItem) const; TListItem* GetItem(LONG index); T& GetItemValue() { return m_Tinfo; } // additional member function TListItem* MergeSort(BOOL (* fcnCompare) (T&, T&)); // Destructively mergeSorts the list items private: void Divide(TListItem* &pHalf1, TListItem* &pHalf2); TListItem* Merge(TListItem* pOtherList, BOOL (* fcnCompare) (T&, T&)); T m_Tinfo; // additional data member, but memory is the same since in AListItem // you put the extra data member in the derived class TListItem *m_pNext; }; // TList<> adds a destructor to AList. template class TList { public: TList() {m_pHead=NULL;} ~TList() { //if (m_pHead != NULL) delete m_pHead; TListItem::Delete(m_pHead); } // new destructor TListItem *GetHead() const { return m_pHead;}; void RemoveAll() { m_pHead=NULL;}; void CleanUp() { //if (m_pHead) delete m_pHead; if (m_pHead) TListItem::Delete(m_pHead); m_pHead=NULL; } LONG GetCount() const {return m_pHead->GetCount();}; TListItem *GetItem(LONG index) { return m_pHead->GetItem(index);}; void InsertBefore(TListItem *pItem,TListItem *pInsert); void Cat(TListItem *pItem) {m_pHead=m_pHead->Cat(pItem);}; void Cat(TList *pList) { // assert(pList!=NULL); m_pHead=m_pHead->Cat(pList->GetHead()); }; void AddHead(TListItem *pItem) { // assert(pItem!=NULL); pItem->SetNext(m_pHead); m_pHead=pItem; } void AddTail(TListItem *pItem);// {m_pHead=m_pHead->AddTail(pItem);}; void Remove(TListItem *pItem) {m_pHead=m_pHead->Remove(pItem);}; TListItem *GetPrev(TListItem *pItem) const {return m_pHead->GetPrev(pItem);}; TListItem *GetTail() const {return GetPrev(NULL);}; BOOL IsEmpty(void) const {return (m_pHead==NULL);}; TListItem *RemoveHead(void) { TListItem *li; li=m_pHead; if(m_pHead) { m_pHead=m_pHead->GetNext(); li->SetNext(NULL); } return li; } void MergeSort(BOOL (* fcnCompare) (T&, T&)); // Destructively mergeSorts the list void Reverse(void); // Reverses the entire list HRESULT Copy(TList& rList); // Copies one list to another protected: TListItem *m_pHead; }; #include "tlist.cpp" #endif // __TLIST_H__