-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.h
More file actions
189 lines (162 loc) · 4.77 KB
/
Copy pathlist.h
File metadata and controls
189 lines (162 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Created by ssln on 2019/12/18.
//
#ifndef ALGORITHM_LIST_H
#define ALGORITHM_LIST_H
/**
* offset_of(TYPE, MEMBER)
*
* @type: The type of the structure
* @member: The field to return the offset
*/
#define offset_of(type, member) ((size_t) &((type *)0)->member)
/**
* sizeof_field(TYPE, MEMBER)
*
* @type: The type of the structure
* @member: The field to return the size of
*/
#define sizeof_field(type, member) (sizeof(((type*)0)->member))
/**
* offset_of_end(TYPE, MEMBER)
*
* @type: The type of the structure
* @member: The member within the structure to get the end offset of
*/
#define offset_of_end(type, member) \
(offset_of(type, member) + sizeof_field(type, member))
/**
* container_of - Cast a member of a structure out to the containing structure
* @ptr: The pointer to the member.
* @type: The type of the container struct this is embedded in.
* @member: The name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
void* __mptr = void* (ptr); \
((type *)(__mptr - offsetof(type, member))); })
namespace algorithm
{
template <class T>
struct link
{
link(){}
link(const T& data) : data(data) {}
T data;
link* prev = nullptr, *next = nullptr;
};
/// <summary>
/// Circular linked list
/// </summary>
/// <typeparam name="T">data base type</typeparam>
template <class T>
class list
{
public:
list()
{
init_list_head();
}
void insert(int index, const T& data)
{
if(1 <= index)
{
int step = 1;
auto ptr = head_ptr;
while ((ptr = ptr->next) != head_ptr && step < index)
{
++step;
}
__list_add(new link<T>(data), ptr->prev, ptr);
}
}
void append(const T& data)
{
//add tail node, the previous node of the new added node should be previoud node of head node,
//and next node of new added node should be head node
__list_add(new link<T> (data), head_ptr->prev, head_ptr);
}
void push(const T& data)
{
//push node, the previous node of the new node should be head node,
//and the tail node should be next node of head
__list_add(new link<T> (data), head_ptr, head_ptr->next);
}
void pop()
{
//pop node, the node should be next node of head
link<T>* ptr = head_ptr->next;
__list_del(ptr->prev, ptr->next);
delete ptr;
ptr = nullptr;
}
void pop(link<T>* ptr)
{
ptr = head_ptr->next;
__list_del(ptr->prev, ptr->next);
ptr->prev = nullptr;
ptr->next = nullptr;
}
link<T>* head() const
{
return head_ptr;
}
link<T>* tail() const
{
return head_ptr->prev;
}
void print()
{
for (auto ptr = head_ptr->next; ptr != head_ptr; ptr = ptr->next) {
std::cout << ptr->data;
if(ptr->next != head_ptr)
std::cout << "<-->";
}
std::cout << std::endl;
}
void reverse_print()
{
for (auto ptr = head_ptr->prev; ptr != head_ptr; ptr = ptr->prev)
{
std::cout << ptr->data;
if (ptr->prev != head_ptr)
std::cout << "<-->";
}
std::cout << std::endl;
}
private:
void init_list_head()
{
//Create head node
head_ptr = new link<T>();
head_ptr->prev = head_ptr;
head_ptr->next = head_ptr;
}
/// <summary>
/// Add a new node into nodelist
/// </summary>
/// <param name="node">new node</param>
/// <param name="prev">previous node of added node</param>
/// <param name="next">next node of added node</param>
static void __list_add(link<T> *node, link<T> *prev, link<T> *next)
{
node->next = next;
node->prev = prev;
prev->next = node;
next->prev = node;
}
/// <summary>
/// Delete specified node
/// </summary>
/// <param name="prev">previous node of the deleted node</param>
/// <param name="next">next node of the deleted node</param>
static void __list_del(link<T> *prev, link<T> *next)
{
next->prev = prev;
prev->next = next;
}
private:
link<T> * head_ptr = nullptr;
};
}
#endif //ALGORITHM_LIST_H