www.gusucode.com > VC++三子棋游戏源码(类似五子棋)-源码程序 > VC++三子棋游戏源码(类似五子棋)-源码程序\code\Collection.cpp
//Collection.cpp /////////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include "Collection.h" /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// // Download by http://www.NewXing.com Node::Node() { m_Prev = m_Next = NULL; m_Object = NULL; } Node::~Node() { } void * Node::operator=(void * ptr) { m_Object = ptr; return ptr; } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// //class Collection Collection::Collection() { m_Head = m_Tail = NULL; m_Length = 0; } Collection::~Collection() { RemoveObjectsAndNodes(); } // get head Node * Collection::Head() { return m_Head; } // get tail Node * Collection::Tail() { return m_Tail; } // get length of Q ulong Collection::Length() { return m_Length; } // add node to head boolean Collection::AddObjectToTail(void * object) { if(object == NULL) return FALSE; Node * pNode = new Node(); pNode->m_Object = object; if(m_Tail == NULL) { m_Head = pNode; } else { m_Tail->m_Next = pNode; pNode->m_Prev = m_Tail; } m_Tail = pNode; m_Length++; return TRUE; } // add node to tail boolean Collection::AddObjectToHead(void * object) { if(object == NULL) return FALSE; Node * pNode = new Node(); pNode->m_Object = object; if(m_Head == NULL) { m_Tail = pNode; } else { m_Head->m_Prev = pNode; pNode->m_Next = m_Head; } m_Head = pNode; m_Length++; return TRUE; } //--------------------------------------------------------------------------- // Delete perticular object. If mgr is not NULL, add object to the manager's // garbage collector bin. boolean Collection::RemoveObject(void * object) { if(object == NULL) return FALSE; Node * pNode = Head(); for(ulong i=0; i<m_Length; i++) { if(object == pNode->m_Object) { if(pNode->m_Prev) pNode->m_Prev->m_Next = pNode->m_Next; if(pNode->m_Next) pNode->m_Next->m_Prev = pNode->m_Prev; if(m_Head == pNode) // Head item. m_Head = pNode->m_Next; if(m_Tail == pNode) // Tail item. m_Tail = pNode->m_Prev; m_Length--; // Whenever object is added to list, it's duplicated. // So release it whenever it's removed from list. Node * oldNode = pNode; pNode = pNode->m_Next; delete oldNode; // delete node return TRUE; } else { pNode = pNode->m_Next; } } return FALSE; } //--------------------------------------------------------------------------- // Delete all nodes. boolean Collection::RemoveAllNodes() { Node * pNode = Head(); for(ulong i=0; i<m_Length; i++) { if(pNode) { Node * oldNode = pNode; pNode = pNode->m_Next; delete oldNode; // delete node } } m_Head = m_Tail = NULL; m_Length = 0; return TRUE; } //--------------------------------------------------------------------------- // Delete all objects and nodes. boolean Collection::RemoveObjectsAndNodes() { Node * pNode = Head(); for(ulong i=0; i<m_Length; i++) { if(pNode) { Node * oldNode = pNode; pNode = pNode->m_Next; delete oldNode; // delete node } } m_Head = m_Tail = NULL; m_Length = 0; return TRUE; } //--------------------------------------------------------------------------- void * Collection::GetObject(ulong index) { if(index >= m_Length) return NULL; Node * pNode = Head(); for(ulong i=0; i<m_Length; i++) { if(i == index) return pNode->m_Object; pNode = pNode->m_Next; } return NULL; } //--------------------------------------------------------------------------- void * Collection::operator[](ulong index) { return GetObject(index); } //---------------------------------------------------------------------------