线性循环链表

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值