Skip to content

Commit 50638ea

Browse files
committed
Adding LinkedList2 that uses intrusive nodes
1 parent bc791c5 commit 50638ea

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

LinkedList.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,67 @@ class LinkedList {
5555
Node* ret = n->next;
5656
delete n;
5757
length -= 1;
58-
return n->next;
58+
return ret;
5959
}
6060

6161
Node* last;
6262
Node* first;
6363
uint16_t length;
6464
};
6565

66+
67+
template<typename NodeType>
68+
class LinkedList2{
69+
public:
70+
LinkedList2():last(NULL),first(NULL),length(0){}
71+
72+
void Add(NodeType* item) {
73+
if (last == NULL) {
74+
last = item;
75+
first = item;
76+
item->next = NULL;
77+
item->prev = NULL;
78+
} else {
79+
last->next = item;
80+
item->prev = last;
81+
item->next = NULL;
82+
last = item;
83+
}
84+
length += 1;
85+
}
86+
87+
template<typename SearchParamType>
88+
NodeType* Find(SearchParamType input, bool(*search_func)(const NodeType*, SearchParamType))
89+
{
90+
NodeType* current = first;
91+
while (current != NULL) {
92+
if (search_func(current, input))
93+
return current;
94+
current = current->next;
95+
}
96+
return NULL;
97+
}
98+
99+
NodeType* Remove(NodeType* n) {
100+
if (!n)
101+
return NULL;
102+
if (first == n)
103+
first = n->next;
104+
if (last == n)
105+
last = n->prev;
106+
if (n->prev)
107+
n->prev->next = n->next;
108+
if (n->next)
109+
n->next->prev = n->prev;
110+
NodeType* ret = n->next;
111+
delete n;
112+
length -= 1;
113+
return ret;
114+
}
115+
116+
NodeType* last;
117+
NodeType* first;
118+
uint16_t length;
119+
};
120+
66121
#endif

0 commit comments

Comments
 (0)