template <class Type> LISTNODE List<Type>::AddSorted(const Type &item, LPVOID pfn) { ListNode<Type> *pNode = NULL; LISTNODE pCurrNode = NULL; LISTNODE pPrevNode = NULL; int i; Type curItem; LONG (*pFN) (const Type item1, const Type item2); pFN = (LONG (*) (const Type item1, const Type item2))pfn; if(_pHead == NULL) { return AddHead(item); } else { pCurrNode = GetHeadPosition(); curItem = ((ListNode<Type> *) pCurrNode)->GetItem(); for (i = 0; i < _iCount; i++) { if (pFN(item, curItem) < 1) { NEW_CONSTR(pNode, ListNode<Type>(item)); if (pNode) { pNode->SetPrev((ListNode<Type> *)pPrevNode); pNode->SetNext((ListNode<Type> *)pCurrNode); // update pPrevNode if(pPrevNode) { ((ListNode<Type> *)pPrevNode)->SetNext(pNode); } else { _pHead = pNode; } // update pCurrNode ((ListNode<Type> *)pCurrNode)->SetPrev(pNode); _iCount++; } break; } pPrevNode = pCurrNode; GetNext(pCurrNode); if(i+1 == _iCount) { return AddTail(item); } else { _ASSERTE(pCurrNode); curItem = GetAt(pCurrNode); } } } return (LISTNODE)pNode; }
template <class Type> LISTNODE List<Type>::AddHead(const Type &item) { ListNode<Type> *pNode = NULL; NEW_CONSTR(pNode, ListNode<Type>(item)); if (pNode) { _iCount++; pNode->SetNext(_pHead); pNode->SetPrev(NULL); if (_pHead == NULL) { _pTail = pNode; } else { _pHead->SetPrev(pNode); } _pHead = pNode; } return (LISTNODE)pNode; }
template <class Type> void List<Type>::RemoveAt(LISTNODE pNode) { ListNode<Type> *pListNode = (ListNode<Type> *)pNode; ListNode<Type> *pPrevNode = NULL; ListNode<Type> *pNextNode = NULL; if (pNode) { pPrevNode = pListNode->GetPrev(); pNextNode = pListNode->GetNext(); if (pPrevNode) { pPrevNode->SetNext(pNextNode); if (pNextNode) { pNextNode->SetPrev(pPrevNode); } else { // We're removing the last node, so we have a new tail _pTail = pPrevNode; } SAFE_DELETE(pListNode); } else { // No previous, so we are the head of the list _pHead = pNextNode; if (pNextNode) { pNextNode->SetPrev(NULL); } else { // No previous, or next. There was only one node. _pHead = NULL; _pTail = NULL; } SAFE_DELETE(pListNode); } _iCount--; } }