#include <cstring> template <class Tp> struct ListNode { Tp data; ListNode* next; ListNode* prev; }; template <class Tp> class List { public: List() { m_node = new ListNode<Tp>; m_node->next = m_node; m_node->prev = m_node; } ~List() { clear(); delete m_node; } void push_back(const Tp& x) { ListNode<Tp>* tmp = new ListNode<Tp>; tmp->data = x; tmp->next = m_node; tmp->prev = m_node->prev; m_node->prev->next = tmp; m_node->prev = tmp; } void pop_front() { ListNode<Tp>* begin = m_node->next; begin->prev->next = begin->next; begin->next->prev = begin->prev; delete begin; } Tp front() { return m_node->next->data; } bool empty() { return m_node->next == m_node; } void clear() { ListNode<Tp>* cur = m_node->prev; while (cur != m_node) { ListNode<Tp>* tmp = cur; cur = cur->next; delete tmp; } m_node->next = m_node; m_node->prev = m_node; } private: ListNode<Tp>* m_node; };
标签:node,tmp,begin,ListNode,list,next,prev From: https://www.cnblogs.com/ho966/p/17462089.html