?? dlist.h
字號:
#ifndef DLIST_H
#define DLIST_H
#include <cassert>
template<class T>
class Node
{
public:
T data;
Node<T> *prior;
Node<T> *next;
Node() : data(T()), prior(0), next(0) {}
Node(const T &initdata) : data(initdata), prior(0), next(0) {}
};
template<class T>
class DList
{
protected:
int m_nCount;
Node<T> *m_pNodeHead;
Node<T> *m_pNodeTail;
public:
DList();
DList(const T &initdata);
~DList();
public:
int IsEmpty() const;
int GetCount() const;
int InsertBefore(const int pos, const T data);
int InsertAfter(const int pos, const T data);
int AddHead(const T data);
int AddTail(const T data);
void RemoveAt(const int pos);
void RemoveHead();
void RemoveTail();
void RemoveAll();
T& GetTail();
T GetTail() const;
T& GetHead();
T GetHead() const;
T& GetAt(const int pos);
T GetAt(const int pos) const;
void SetAt(const int pos, T data);
int Find(const T data) const;
T& GetPrev(int &pos);
T& GetNext(int &pos);
};
template<class T>
inline DList<T>::DList() : m_nCount(0), m_pNodeHead(0), m_pNodeTail(0)
{
}
template<class T>
inline DList<T>::DList(const T &initdata)
: m_nCount(0), m_pNodeHead(0), m_pNodeTail(0)
{
AddHead(initdata);
}
template<class T>
inline DList<T>::~DList()
{
RemoveAll();
}
template<class T>
inline T& DList<T>::GetNext(int &pos)
{
assert(0 != m_nCount);
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
++pos;
return pTmpNode->data;
}
template<class T>
inline T& DList<T>::GetPrev(int &pos)
{
assert(0 != m_nCount);
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
--pos;
return pTmpNode->data;
}
template<class T>
inline int DList<T>::InsertBefore(const int pos, const T data)
{
int i;
int nRetPos;
Node<T> *pTmpNode;
Node<T> *pNewNode;
pNewNode = new Node<T>;
if (0 == pNewNode)
{
nRetPos = 0;
return nRetPos;
}
pNewNode->data = data;
// if the list is empty, replace the head node with the new node.
if (0 == m_pNodeHead)
{
pNewNode->prior = 0;
pNewNode->next = 0;
m_pNodeHead = pNewNode;
m_pNodeTail = pNewNode;
nRetPos = 1;
m_nCount++;
return nRetPos;
}
// is pos range valid?
assert(1 <= pos && pos <= m_nCount);
// insert before head node?
if (1 == pos)
{
pNewNode->prior = 0;
pNewNode->next = m_pNodeHead;
m_pNodeHead->prior = pNewNode;
m_pNodeHead = pNewNode;
nRetPos = 1;
m_nCount++;
return nRetPos;
}
// if the list is not empty and is not inserted before head node,
// seek to the pos of the list and insert the new node before it.
pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
pNewNode->next = pTmpNode;
pNewNode->prior = pTmpNode->prior;
pTmpNode->prior->next = pNewNode;
pTmpNode->prior = pNewNode;
// if tail node, must update m_pNodeTail
if (0 == pNewNode->next)
{
m_pNodeTail = pNewNode;
}
nRetPos = pos;
++m_nCount;
return nRetPos;
}
template<class T>
inline int DList<T>::InsertAfter(const int pos, const T data)
{
int i;
int nRetPos;
Node<T> *pNewNode;
Node<T> *pTmpNode;
pNewNode = new Node<T>;
if (0 == pNewNode)
{
nRetPos = 0;
return nRetPos;
}
pNewNode->data = data;
// if the list is empty, replace the head node with the new node.
if (0 == m_pNodeHead)
{
pNewNode->prior = 0;
pNewNode->next = 0;
m_pNodeHead = pNewNode;
m_pNodeTail = pNewNode;
nRetPos = 1;
m_nCount++;
return nRetPos;
}
// is pos range valid?
assert(1 <= pos && pos <= m_nCount);
// if the list is not empty,
// seek to the pos of the list and insert the new node after it.
pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
pNewNode->next = pTmpNode->next;
pNewNode->prior = pTmpNode;
// if NewNode's position is m_pNodeTail, update m_pNodeTail
if (pTmpNode->next == m_pNodeTail)
{
m_pNodeTail->prior = pNewNode;
}
pTmpNode->next = pNewNode;
// if tail node, must update m_pNodeTail
if (0 == pNewNode->next)
{
m_pNodeTail = pNewNode;
}
nRetPos = pos + 1;
++m_nCount;
return nRetPos;
}
template<class T>
inline T& DList<T>::GetAt(const int pos)
{
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
return pTmpNode->data;
}
template<class T>
inline T DList<T>::GetAt(const int pos) const
{
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
return pTmpNode->data;
}
template<class T>
inline int DList<T>::AddHead(const T data)
{
return InsertBefore(1, data);
}
template<class T>
inline int DList<T>::AddTail(const T data)
{
return InsertAfter(GetCount(), data);
}
template<class T>
inline int DList<T>::IsEmpty() const
{
return 0 == m_nCount;
}
template<class T>
inline int DList<T>::GetCount() const
{
return m_nCount;
}
template<class T>
inline T& DList<T>::GetTail()
{
assert(0 != m_nCount);
return m_pNodeTail->data;
}
template<class T>
inline T DList<T>::GetTail() const
{
assert(0 != m_nCount);
return m_pNodeTail->data;
}
template<class T>
inline T& DList<T>::GetHead()
{
assert(0 != m_nCount);
return m_pNodeHead->data;
}
template<class T>
inline T DList<T>::GetHead() const
{
assert(0 != m_nCount);
return m_pNodeHead->data;
}
template<class T>
inline void DList<T>::RemoveAt(const int pos)
{
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode = m_pNodeHead;
// head node?
if (1 == pos)
{
m_pNodeHead = m_pNodeHead->next;
}
else
{
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
pTmpNode->prior->next = pTmpNode->next;
}
delete pTmpNode;
--m_nCount;
if (0 == m_nCount)
{
m_pNodeTail = 0;
}
}
template<class T>
inline void DList<T>::RemoveHead()
{
assert(0 != m_nCount);
RemoveAt(1);
}
template<class T>
inline void DList<T>::RemoveTail()
{
assert(0 != m_nCount);
RemoveAt(m_nCount);
}
template<class T>
inline void DList<T>::RemoveAll()
{
int i;
int nCount;
Node<T> *pTmpNode;
nCount = m_nCount;
for (i = 0; i < nCount; ++i)
{
pTmpNode = m_pNodeHead->next;
delete m_pNodeHead;
m_pNodeHead = pTmpNode;
}
m_nCount = 0;
}
template<class T>
inline void DList<T>::SetAt(const int pos, T data)
{
assert(1 <= pos && pos <= m_nCount);
int i;
Node<T> *pTmpNode = m_pNodeHead;
for (i = 1; i < pos; ++i)
{
pTmpNode = pTmpNode->next;
}
pTmpNode->data = data;
}
template<class T>
inline int DList<T>::Find(const T data) const
{
int i;
int nCount;
Node<T> *pTmpNode = m_pNodeHead;
nCount = m_nCount;
for (i = 0; i < nCount; ++i)
{
if (data == pTmpNode->data)
return i + 1;
pTmpNode = pTmpNode->next;
}
return 0;
}
#endif//DLIST_H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -