/*++ Copyright (c) 1998 Microsoft Corporation All rights reserved. Module Name: dlist.inl Abstract: Double linked list template class. Author: Weihai Chen (WeihaiC) 06/29/98 Revision History: Weihai Chen (WeihaiC) 03/08/00 Rename to T* --*/ template TDoubleList::TDoubleList(void): m_pHead (NULL), m_pTail (NULL), m_dwNumNode (0), m_bValid (TRUE) { } template TDoubleList::~TDoubleList(void) { TDoubleNode *pNode = m_pHead; TDoubleNode *pNext; while (pNode) { pNext = pNode->GetNext(); delete pNode; pNode = pNext; } } template BOOL TDoubleList::InsertItem (T item) { BOOL bRet = FALSE; TDoubleNode *pNewNode = new TDoubleNode (item); if (pNewNode) { bRet = InsertNode (pNewNode); } return bRet; } template BOOL TDoubleList::AppendItem (T item) { BOOL bRet = FALSE; TDoubleNode *pNewNode = new TDoubleNode (item); if (pNewNode) { bRet = AppendNode (pNewNode); } return bRet; } template BOOL TDoubleList::DeleteItem (T item) { BOOL bRet = FALSE; TDoubleNode *pNode = m_pHead; while (pNode) { if (pNode->IsSameItem (item)) { // Delete it DeleteNode (pNode); delete (item); bRet = TRUE; break; } pNode = pNode->GetNext(); } return bRet; } template T TDoubleList::GetItemFromIndex (DWORD dwIndex) { TDoubleNode *pNode = GetNodeFromIndex (dwIndex); if (pNode) { return pNode->GetData(); } return NULL; } template TDoubleNode * TDoubleList::GetNodeFromIndex (DWORD dwIndex) { TDoubleNode *pNode = m_pHead; DWORD i = 0; while (pNode && i < dwIndex) { i++; pNode = pNode->GetNext(); } return pNode; } template BOOL TDoubleList::InsertNode (TDoubleNode *pNewNode) { BOOL bRet = FALSE; if (pNewNode) { if (m_pHead) { pNewNode->SetNext (m_pHead); pNewNode->SetPrev (NULL); m_pHead->SetPrev (pNewNode); } else { // This is the first node pNewNode->SetNext (NULL); pNewNode->SetPrev (NULL); m_pTail = pNewNode; } m_pHead = pNewNode; m_dwNumNode ++; bRet = TRUE; } return bRet; } template BOOL TDoubleList::AppendNode (TDoubleNode *pNewNode) { BOOL bRet = FALSE; if (pNewNode) { if (m_pTail) { pNewNode->SetNext (NULL); pNewNode->SetPrev (m_pTail); m_pTail->SetNext (pNewNode); } else { // This is the first node pNewNode->SetNext (NULL); pNewNode->SetPrev (NULL); m_pHead = pNewNode; } m_pTail = pNewNode; m_dwNumNode ++; bRet = TRUE; } return bRet; } template BOOL TDoubleList::DeleteNode (TDoubleNode *pNode) { TDoubleNode *pNextNode; TDoubleNode *pPrevNode; BOOL bRet = FALSE; if (pNode) { // It is not the first one, i.e. not the only one pNextNode = pNode->GetNext (); pPrevNode = pNode->GetPrev (); if (pNextNode) { pNextNode->SetPrev (pPrevNode); } else { m_pTail = pPrevNode; } if (pPrevNode) { pPrevNode->SetNext (pNextNode); } else { // It is the first one m_pHead = pNextNode; } m_dwNumNode --; bRet = TRUE; } return bRet; } template TDoubleNode * TDoubleList::GetHead (void) { TDoubleNode * pHead = NULL; pHead = m_pHead; return pHead; } template BOOL TDoubleList::GetTotalNode (PDWORD pdwCount) { BOOL bRet = FALSE; if (m_bValid) { *pdwCount = m_dwNumNode; bRet = TRUE; } return bRet; } template T TSrchDoubleList::FindItemFromItem (T item) { TDoubleNode *pNode = FindNodeFromItem (item); if (pNode) { return pNode->GetData(); } return NULL; } template T TSrchDoubleList::FindItemFromKey (KEYTYPE key) { TDoubleNode *pNode = FindNodeFromKey (key); if (pNode) { return pNode->GetData(); } return NULL; } template TDoubleNode * TSrchDoubleList::FindNodeFromItem (T item) { TDoubleNode *pNode = m_pHead; while (pNode) { if (pNode->IsSameItem (item)) { return pNode; } pNode = pNode->GetNext(); } return NULL; } template TDoubleNode * TSrchDoubleList::FindNodeFromKey (KEYTYPE key) { TDoubleNode *pNode = m_pHead; while (pNode) { if (pNode->IsSameKey (key)) { return pNode; } pNode = pNode->GetNext(); } return NULL; }