template<typename T>
struct LNode
{
T data;
struct LNode<T> *next;
};
template<typename T>
class CircularLinkList
{
private:
LNode<T>* m_pList;
int m_ListLength;
private:
bool InitList();
bool GetNode(int position, LNode<T>** node);
public:
CircularLinkList();
~CircularLinkList();
bool DestroyList();
bool InsertNode(int beforeWhich, T data);
bool DeleteNode(int position);
bool IsEmpty();
int GetLength();
bool ClearList();
bool SetNodeData(int position, T NewData);
bool GetNodeData(int position, T& data);
};
template<typename T>
CircularLinkList<T>::CircularLinkList()
{
m_pList = NULL;
m_ListLength = 0;
InitList();
}
template<typename T>
CircularLinkList<T>::~CircularLinkList()
{
if (!DestroyList())
{
DestroyList();
}
}
template<typename T>
bool CircularLinkList<T>::InitList()
{
m_pList = new LNode<T>;
if (!m_pList)
{
return false;
}
m_pList->data = NULL;
m_pList->next = m_pList;
return true;
}
template<typename T>
bool CircularLinkList<T>::DestroyList()
{
if (ClearList())
{
return false;
}
delete m_pList;
m_pList = NULL;
return true;
}
//在链表中插入一个节点,插入之后在链表中的位置为beforeWhich
template<typename T>
bool CircularLinkList<T>::InsertNode(int beforeWhich, T data)
{
LNode<T> *pPrevious = NULL;
if (beforeWhich < 1 || beforeWhich >(m_ListLength + 1))
{
return false;
}
if (!GetNode(beforeWhich - 1, &pPrevious))
{
return false;
}
LNode<T> *newNode = new LNode<T>;
newNode->data = data;
newNode->next = pPrevious->next;
pPrevious->next = newNode;
m_ListLength++;
return true;
}
//删除链接中的指定节点
template<typename T>
bool CircularLinkList<T>::DeleteNode(int position)
{
if (position < 1 || position > m_ListLength)
{
return false;
}
LNode<T> *pPrevious = NULL;
if (!(GetNode(position - 1, &pPrevious)))
{
return false;
}
LNode<T> *pCurrent = pPrevious->next;
pPrevious->next = pCurrent->next;
delete pCurrent;
pCurrent = NULL;
m_ListLength--;
return true;
}
//获取链表中指定的节点
template<typename T>
bool CircularLinkList<T>::GetNode(int position, LNode<T>** node)
{
if (position < 0 || position > m_ListLength)
{
return false;
}
LNode<T> *pNext = m_pList;
for (int i = 1; i <= position; i++)
{
pNext = pNext->next;
}
*node = pNext;
return true;
}
//判断链表是否为空
template<typename T>
bool CircularLinkList<T>::IsEmpty()
{
if (m_pList->next == m_pList)
{
return true;
}
return false;
}
//获取链表中节点的个数
template<typename T>
int CircularLinkList<T>::GetLength()
{
return m_ListLength;
}
//清空链表
template<typename T>
bool CircularLinkList<T>::ClearList()
{
if (m_pList == NULL)
{
return false;
}
LNode<T> *pNode = m_pList->next;
LNode<T> *pTemp = NULL;
while (pNode != m_pList)
{
pTemp = pNode;
pNode = pNode->next;
delete pTemp;
pTemp = NULL;
}
m_pList->next = m_pList;
m_ListLength = 0;
return true;
}
//设置链表中指定节点的数据
template<typename T>
bool CircularLinkList<T>::SetNodeData(int position, T newData)
{
LNode<T> *pNode = NULL;
if (!GetNode(position, &pNode))
{
return false;
}
pNode->data = newData;
return true;
}
//获取链表中指定节点的数据
template<typename T>
bool CircularLinkList<T>::GetNodeData(int position, T& data)
{
LNode<T> *pNode = NULL;
if (!GetNode(position, &pNode))
{
return false;
}
data = pNode->data;
return true;
}
线性循环链表
最新推荐文章于 2023-04-12 10:22:49 发布