前言
&emsp 此前有读者问我,C++的STL中为什么没有循环链表?我想大概率是因为循环链表的应用很少,另外如果使用者对循环链表的理解不足的话还很容易让程序陷入死循环。
代码实现
#pragma once
/*
*实现循环链表
*/
template <typename T>
struct ListNode
{
ListNode* m_next;
ListNode* m_prev;
T m_data;
ListNode(T x)
:m_next(nullptr)
, m_prev(nullptr)
, m_data(x)
{}
};
template <typename T>
class CicList {
typedef ListNode<T> Node;
public:
CList()
:_head(new Node(T()))
{
_head->_next = _head;
_head->_prev = _head;
}
virtual ~CList()
{
Node *tmp = _head->_next;
while (tmp != _head)
{
Node *cur = tmp->_next;
delete tmp;
tmp = cur;
}
}
void setHead(T x) {
_head->_data = x;
}
void push_front(const T & x) {
Insert(_head->_next, x);
}
void push_back(const T & x) {
Insert(_head->_prev, x);
}
private:
//插入一个元素
void Insert(Node* pos, T x)
{
Node *newnode = new Node(x);
newnode->m_next = pos;
pos->m_prev->m_next = newnode;
newnode->m_prev = pos->m_prev;
pos->m_prev = newnode;
}
//data
Node* m_head;
};