📄 list.cpp
字号:
//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Remove the node number iNode from the list and return a pointer to it.// As for array, the index of the first node is 0.//------------------------------------------------------------------------------template <class T> T* C_List<T>::Remove(unsigned int iNode){ // Avoid problems if the list is empty or if the programmer is stupid ASSERT(iNode < iNodeNumber); // Find the node C_ListNode<T>* pNode = pFirst->pNext; for(unsigned int iIndex = 0; iIndex < iNode; iIndex++) { pNode = pNode->pNext; } ASSERT(pNode); // Extract the node from the list pNode->pPrevious->pNext = pNode->pNext; pNode->pNext->pPrevious = pNode->pPrevious; // Decrease the node counter iNodeNumber--; T* pItem = pNode->pData; pNode->pData = NULL; delete pNode; return pItem;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Remove the node number iNode from the list and delete it.// As for array, the index of the first node is 0.//------------------------------------------------------------------------------template <class T> void C_List<T>::Delete(unsigned int iNode){ // Remove the item from the list T* pItem = Remove(iNode); ASSERT(pItem); // Delete it delete pItem;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns the index of the T object is the list or GEN_ERR if the object is not// in the list// Use the == operator of the T class as the predicate of equality//------------------------------------------------------------------------------template <class T> int C_List<T>::Find(const T& cItem) const{ C_ListNode<T>* pNode = pFirst->pNext; for(unsigned int iIndex = 0; iIndex < iNodeNumber; iIndex++) { if(*pNode->pData == cItem) return iIndex; else pNode = pNode->pNext; }; // Node was not found return GEN_ERR;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> unsigned int C_List<T>::Size() const{ return iNodeNumber;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// As for arrays, the index of the first node is 0//------------------------------------------------------------------------------template <class T> T& C_List<T>::operator [] (unsigned int iNode) const{ // Avoid problems if the list is empty or if the programmer is lazy ASSERT(iNode < iNodeNumber); // Start with the first real node C_ListNode<T>* pNode = pFirst->pNext; for(unsigned int iIndex = 0; iIndex < iNode; iIndex++) { pNode = pNode->pNext; } return *(pNode->pData);}//------------------------------------------------------------------------------// Removal of elements is done according to the bAutoClean flag //------------------------------------------------------------------------------template <class T> void C_List<T>::Empty(){ switch(m_bAutoClean) { case YES: { // Go through the list of nodes and delete them C_ListNode<T>* pNode = pFirst->pNext; while (pNode->pNext != NULL) { pNode = pNode->pNext; delete pNode->pPrevious; } break; } case NO: { // Go through the list of nodes but just delete the C_ListNode objects C_ListNode<T>* pNode = pFirst->pNext; while (pNode->pNext != NULL) { pNode->pData = NULL; pNode = pNode->pNext; delete pNode->pPrevious; } break; } case SMART: { // Delete the items as for YES, but also detect if an item is // stored twice to avoid to delete it more than once C_ListNode<T>* pBaseNode = pFirst->pNext; while(pBaseNode != pLast) { C_ListNode<T>* pCurrentNode = pBaseNode->pNext; // Delete the first occurence of the last base item delete pCurrentNode->pPrevious; // Remove all the copies of the base node while(pCurrentNode != pLast) { if(pCurrentNode->pData == pBaseNode->pData) { pCurrentNode->pData = NULL; pCurrentNode->pPrevious->pNext = pCurrentNode->pNext; pCurrentNode->pNext->pPrevious = pCurrentNode->pPrevious; delete pCurrentNode; } pCurrentNode = pCurrentNode->pNext; } // Delete the first occurence of the current item pBaseNode = pCurrentNode->pNext; delete pCurrentNode->pPrevious; } break; } default: { ASSERT(false); break; } }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_ListIterator<T> C_List<T>::CreateIterator() const{ return C_ListIterator<T>(*this);}//******************************************************************************// class C_ListIterator//******************************************************************************// //******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_ListIterator<T>::C_ListIterator(const C_List<T>& cList) : m_cList(cList){ Reset();}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> bool C_ListIterator<T>::HasNext(){ ASSERT(m_pCurrentNode); return m_pCurrentNode->pNext != NULL;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> T* C_ListIterator<T>::GetNext(){ ASSERT(m_pCurrentNode); T* pResult = m_pCurrentNode->pData; ASSERT(pResult); m_pCurrentNode = m_pCurrentNode->pNext; return pResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> bool C_ListIterator<T>::HasPrevious(){ ASSERT(m_pCurrentNode); return m_pCurrentNode->pPrevious != NULL;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> T* C_ListIterator<T>::GetPrevious(){ ASSERT(m_pCurrentNode); T* pResult = m_pCurrentNode->pData; ASSERT(pResult); m_pCurrentNode = m_pCurrentNode->pPrevious; return pResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> void C_ListIterator<T>::Reset(){ m_pCurrentNode = m_cList.pFirst->pNext; ASSERT(m_pCurrentNode);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -