我的双向链表模板类

typedef void* POS; // 定位标志
template <class T>
class CIIList
{
protected:
struct SNode {
SNode  *pPrev;
SNode  *pNext;
T      data;
};
SNode* GetNodeAt(int i_nIndex) const;
private:
CIIList(const CIIList& i_yList){}
CIIList& operator = (const CIIList& i_yList){return *this;}
public:
CIIList();
~CIIList();
int GetNum() const
{return m_nNum;}
bool IsEmpty() const
{return m_nNum <= 0;}
bool SetFirst(const T& i_tElement);
bool SetLast(const T& i_tElement);
bool GetFirst(T& o_tElement) const;
T* GetFirst();
bool GetLast(T& o_tElement) const;
T* GetLast();
bool AddFirst(const T& i_tElement); // 在最前增加一个
bool AddLast(const T& i_tElement); // 在最后增加一个
bool RemoveFirst(); // 删除最前一个
bool RemoveLast(); // 删除最后一个
void Empty(); // 清空
POS GetPos(int i_nIndex);
POS GetFirstPos()
{return (POS)m_pNodeHead;}
POS GetLastPos()
{return (POS)m_pNodeTail;}
POS GetNextPos(POS i_pos);
POS GetPrevPos(POS i_pos);
bool GetAt(POS i_pos, T& o_tElement);
T* GetAt(POS i_pos);
bool SetAt(POS i_pos, const T& i_tElement);
bool AddBefore(POS i_pos, const T& i_tElement);
bool AddAfter(POS i_pos, const T& i_tElement);
bool RemoveAt(POS& io_pos);
private:
SNode* m_pNodeHead;
SNode* m_pNodeTail;
int    m_nNum;
};
template <class T>
CIIList<T>::CIIList()
{
m_nNum = 0;
m_pNodeHead = m_pNodeTail = NULL;
}
template <class T>
CIIList<T>::~CIIList()
{
if (!IsEmpty())
Empty();
}
template <class T>
typename CIIList<T>::SNode* CIIList<T>::GetNodeAt(int i_nIndex) const
{
SNode *pNodeOld, *pNodeNew;
int tIndex;
if (i_nIndex < 0 || i_nIndex >= m_nNum)
return NULL;
if (i_nIndex <= m_nNum/2) // 索引位于前半段
{
tIndex = i_nIndex;
pNodeNew = pNodeOld = m_pNodeHead;
while (tIndex-- > 0)
{
pNodeNew = pNodeOld->pNext;
pNodeOld = pNodeNew;
}
}
else // 索引位于后半段
{
tIndex = m_nNum - i_nIndex;
pNodeNew = pNodeOld = m_pNodeTail;
while (--tIndex > 0)
{
pNodeNew = pNodeOld->pPrev;
pNodeOld = pNodeNew;
}
}
return pNodeNew;
}
template <class T>
bool CIIList<T>::SetFirst(const T& i_tElement)
{
if (m_nNum <= 0)
return false;
m_pNodeHead->data = i_tElement;
return true;
}
template <class T>
bool CIIList<T>::SetLast(const T& i_tElement)
{
if (m_nNum <= 0)
return false;
m_pNodeTail->data = i_tElement;
return true;
}
template <class T>
bool CIIList<T>::GetFirst(T& o_tElement) const
{
if (m_nNum <= 0)
return false;
o_tElement = m_pNodeHead->data;
return true;
}
template <class T>
T* CIIList<T>::GetFirst()
{
if (m_nNum <= 0)
return NULL;
return &m_pNodeHead->data;
}
template <class T>
bool CIIList<T>::GetLast(T& o_tElement) const
{
if (m_nNum <= 0)
return false;
o_tElement = m_pNodeTail->data;
return true;
}
template <class T>
T* CIIList<T>::GetLast()
{
if (m_nNum <= 0)
return NULL;
return &m_pNodeTail->data;
}
template <class T>
bool CIIList<T>::AddFirst(const T& i_tElement)
{
SNode *t_pNode = new SNode;
if (m_nNum == 0) // 没有元素...
{
t_pNode->pPrev = NULL;
t_pNode->pNext = NULL;
t_pNode->data = i_tElement;
m_pNodeHead = m_pNodeTail = t_pNode;
}
else // 有元素...
{
t_pNode->pNext = m_pNodeHead;
t_pNode->pPrev = NULL;
t_pNode->data = i_tElement;
m_pNodeHead->pPrev = t_pNode;
m_pNodeHead = t_pNode;
}
t_pNode = NULL;
m_nNum++;
return true;
}
template <class T>
bool CIIList<T>::AddLast(const T& i_tElement)
{
SNode *t_pNode = new SNode;
if (m_nNum == 0) // 没有元素
{
t_pNode->pPrev = NULL;
t_pNode->pNext = NULL;
t_pNode->data = i_tElement;
m_pNodeHead = m_pNodeTail = t_pNode;
}
else // 有元素
{
t_pNode->pNext = NULL;
t_pNode->pPrev = m_pNodeTail;
t_pNode->data = i_tElement;
m_pNodeTail->pNext = t_pNode;
m_pNodeTail = t_pNode;
}
t_pNode = NULL;
m_nNum++;
return true;
}
template <class T>
bool CIIList<T>::RemoveFirst()
{
SNode *t_pNode = NULL;
if (m_nNum == 0)
return false;
if (m_nNum == 1) // 有一个元素
{
delete m_pNodeHead;
m_pNodeHead = m_pNodeTail = NULL;
m_nNum = 0;
}
else // 多于一个元素
{
t_pNode = m_pNodeHead;
m_pNodeHead = t_pNode->pNext;
m_pNodeHead->pPrev = NULL;
delete t_pNode;
t_pNode = NULL;
m_nNum--;
}
return true;
}
template <class T>
bool CIIList<T>::RemoveLast()
{
SNode *t_pNode = NULL;
if (m_nNum == 0)
return false;
if (m_nNum == 1) // 有一个元素
{
delete m_pNodeHead;
m_pNodeHead = m_pNodeTail = NULL;
m_nNum = 0;
}
else // 多于一个元素
{
t_pNode = m_pNodeTail;
m_pNodeTail = t_pNode->pPrev;
m_pNodeTail->pNext = NULL;
delete t_pNode;
t_pNode = NULL;
m_nNum--;
}
return true;
}
template <class T>
void CIIList<T>::Empty()
{
SNode* pNode = m_pNodeHead;
SNode* pNextNode;
while (pNode != NULL)
{
pNextNode = pNode->pNext;
delete pNode;
pNode = pNextNode;
}
m_pNodeHead = m_pNodeTail = NULL;
m_nNum = 0;
}
template <class T>
POS CIIList<T>::GetPos(int i_nIndex)
{
if (i_nIndex < 0 || i_nIndex >= m_nNum) // 索引不能超范围
return NULL;
return (void*)GetNodeAt(i_nIndex);
}
template <class T>
POS CIIList<T>::GetNextPos(POS i_pos)
{
if (i_pos == NULL)
return NULL;
return ((SNode*)i_pos)->pNext;
}
template <class T>
POS CIIList<T>::GetPrevPos(POS i_pos)
{
if (i_pos == NULL)
return NULL;
return ((SNode*)i_pos)->pPrev;
}
template <class T>
T* CIIList<T>::GetAt(POS i_pos)
{
if (i_pos == NULL)
return NULL;
return &((SNode*)i_pos)->data;
}
template <class T>
bool CIIList<T>::GetAt(POS i_pos, T& o_tElement)
{
if (i_pos == NULL)
return false;
o_tElement = ((SNode*)i_pos)->data;
return true;
}
template <class T>
bool CIIList<T>::SetAt(POS i_pos, const T& i_tElement)
{
if (i_pos == NULL)
return false;
((SNode*)i_pos)->data = i_tElement;
return true;
}
template <class T>
bool CIIList<T>::AddBefore(POS i_pos, const T& i_tElement)
{
if (i_pos == NULL)
return false;
SNode* t_pNodeIndex = (SNode*)i_pos;
SNode* t_pNode = new SNode;
t_pNode->data = i_tElement;
t_pNode->pPrev = t_pNodeIndex->pPrev;
t_pNode->pNext = t_pNodeIndex;
if (t_pNode->pPrev != NULL)
(t_pNode->pPrev)->pNext = t_pNode;
else
m_pNodeHead = t_pNode;
t_pNodeIndex->pPrev = t_pNode;
m_nNum++;
return true;
}
template <class T>
bool CIIList<T>::AddAfter(POS i_pos, const T& i_tElement)
{
if (i_pos == NULL)
return false;
SNode* t_pNodeIndex = (SNode*)i_pos;
SNode* t_pNode = new SNode;
t_pNode->data = i_tElement;
t_pNode->pPrev = t_pNodeIndex;
t_pNode->pNext = t_pNodeIndex->pNext;
t_pNodeIndex->pNext = t_pNode;
if (t_pNode->pNext != NULL) // 非尾点
(t_pNode->pNext)->pPrev = t_pNode;
else // 尾点
m_pNodeTail = t_pNode;
m_nNum++;
return true;
}
template <class T>
bool CIIList<T>::RemoveAt(POS& io_pos)
{
if (io_pos == NULL)
return false;
SNode* t_pNode = (SNode*)io_pos;
io_pos = (POS)t_pNode->pNext;
if (t_pNode->pPrev == NULL) // 第一个
{
if (t_pNode->pNext != NULL) // 还有下一个
(t_pNode->pNext)->pPrev = NULL;
else // 没有下一个
m_pNodeTail = NULL;
m_pNodeHead = t_pNode->pNext;
}
else if (t_pNode->pNext == NULL) // 最后一个
{
if (t_pNode->pPrev != NULL) // 还有上一个
(t_pNode->pPrev)->pNext = NULL;
else // 没有上一个
m_pNodeHead = NULL;
m_pNodeTail = t_pNode->pPrev;
}
else // 中间一个
{
(t_pNode->pPrev)->pNext = t_pNode->pNext;
(t_pNode->pNext)->pPrev = t_pNode->pPrev;
}
delete t_pNode;
t_pNode = NULL;
m_nNum--;
return true;
}
posted @ 2012-09-07 22:44  疯狂青蛙  阅读(267)  评论(0编辑  收藏  举报