?? clist.h
字號:
/*
* 循環單鏈表
* FileName:clist.h
*/
#ifndef CLIST_H
#define CLIST_H
#include "../slist/slist.h"
#include <cassert>
template<class T>
class CList : public SList<T>
{
protected:
Node<T> *m_pNodeCurr;
public:
CList();
public:
T& GetNext();
void RemoveAt(const int pos);
int GetCurrentIndex() const;
};
template<class T>
inline T& CList<T>::GetNext()
{
assert(0 != m_nCount);
if ((NULL == m_pNodeCurr) || (NULL == m_pNodeCurr->next))
m_pNodeCurr = m_pHead;
else
m_pNodeCurr = m_pNodeCurr->next;
return m_pNodeCurr->data;
}
template<class T>
inline int CList<T>::GetCurrentIndex() const
{
assert(0 != m_nCount);
int i;
Node<T> *pTmpNode = m_pHead;
for (i = 1; i <= m_nCount; ++i)
{
if (pTmpNode == m_pNodeCurr)
return i;
else
pTmpNode = pTmpNode->next;
}
return 0;
}
template<class T>
inline void CList<T>::RemoveAt(const int pos)
{
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode1;
Node<T> *pTmpNode2;
pTmpNode1 = m_pHead;
// head node?
if (1 == pos)
{
m_pHead = m_pHead->next;
// added for loop list
// m_pNodeCurr will be set to m_pHead in function GetNext()
m_pNodeCurr = NULL;
}
else
{
for (i = 1; i < pos; ++i)
{
// we will get the previous node of the target node after
// the for loop finished, and it would be stored into pTmpNode2
pTmpNode2 = pTmpNode1;
pTmpNode1 = pTmpNode1->next;
}
pTmpNode2->next = pTmpNode1->next;
// added for loop list
m_pNodeCurr = pTmpNode2;
}
delete pTmpNode1;
--m_nCount;
}
template<class T>
inline CList<T>::CList() : m_pNodeCurr(NULL)
{
}
#endif//CLIST_H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -