NANA
list.hpp
浏览该文件的文档.
1#pragma once
16namespace NANA {
17namespace GRAPH {
18
21
22
23template <typename T>
25{
26 ListNode* m_next;
27 ListNode* m_prev;
28 T m_data;
29
30 ListNode(T x)
31 :m_next(nullptr)
32 , m_prev(nullptr)
33 , m_data(x)
34 {}
35};
36
41template <typename T>
42class CicList {
43 typedef ListNode<T> Node;
44public:
45 CicList()
46 :_head(new Node(T()))
47 {
48 _head->_next = _head;
49 _head->_prev = _head;
50 }
51
52
53 virtual ~CicList()
54 {
55 Node* tmp = _head->_next;
56 while (tmp != _head)
57 {
58 Node* cur = tmp->_next;
59 delete tmp;
60 tmp = cur;
61 }
62 }
63
64 void setHead(T x) {
65 _head->_data = x;
66 }
67
68 void push_front(const T& x) {
69 Insert(_head->_next, x);
70 }
71
72 void push_back(const T& x) {
73 Insert(_head->_prev, x);
74 }
75
76
77private:
78 //插入一个元素
79 void Insert(Node* pos, T x)
80 {
81 Node* newnode = new Node(x);
82 newnode->m_next = pos;
83 pos->m_prev->m_next = newnode;
84 newnode->m_prev = pos->m_prev;
85 pos->m_prev = newnode;
86 }
87 //data
88 Node* m_head;
89};
90
95template <typename T>
96class CList {
97 typedef ListNode<T> Node;
98public:
99
100
101protected:
102};
103
104
105
106
108
109}
110}
普通链表,不同于循环链表
Definition: list.hpp:96
双向循环链表
Definition: list.hpp:42