forked from DavidLiRemini/MemoryPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
List_Node.h
70 lines (67 loc) · 1.06 KB
/
List_Node.h
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
#pragma once
#include <assert.h>
template <typename T>
struct List_Node
{
List_Node<T>* prev;
List_Node<T>* next;
T data;
List_Node()
:prev(nullptr),
next(nullptr){}
};
template <typename T>
class ListPool
{
protected:
typedef List_Node<T> ListNode;
public:
typedef ListNode* LinkType;
protected:
LinkType node;
private:
LinkType Create_Node() {
LinkType tmp = new ListNode();
return tmp;
}
public:
ListPool()
{
Empty_initialize();
}
void Empty_initialize()
{
node = Create_Node();
node->prev = node;
node->next = node;
}
void Remove(ListNode& link)
{
assert(link.prev != nullptr);
assert(link.next != nullptr);
link.prev->next = link.next;
link.next->prev = link.prev;
link.next = nullptr;
link.prev = nullptr;
}
void AddNode(const T& x)
{
LinkType tmp = Create_Node();
tmp->data = x;
tmp->next = node;
tmp->prev = node->prev;
node->prev->next = tmp;
node->prev = tmp;
}
unsigned ListSize()const
{
unsigned t = 0;
LinkType ptr = node->next;
while (ptr != node)
{
ptr = ptr->next;
++t;
}
return t;
}
};