forked from yangxu02/LFUCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleLinkedList.cpp
51 lines (41 loc) · 1010 Bytes
/
DoubleLinkedList.cpp
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
#include <iostream>
#include "DoubleLinkedList.h"
Node* DoubleLinkedList::addNode(Node* node, Node* pre, Node* next) {
if (NULL == node) return NULL;
node->pre = pre;
node->next = next;
if (NULL == pre) { // head is NULL
this->head = node;
} else {
pre->next = node;
}
if (NULL == next) { // tail is NULL
this->tail = node;
} else {
next->pre = node;
}
++this->size;
return node;
}
void DoubleLinkedList::removeNode(Node* node) {
if (NULL == node) return;
if (NULL != node->pre) {
node->pre->next = node->next;
}
if (NULL != node->next) {
node->next->pre = node->pre;
}
if (node == this->head) {
this->head = node->next;
if (NULL != this->head) {
this->head->pre = NULL;
}
}
if (node == this->tail) {
this->tail = node->pre;
if (NULL != this->tail) {
this->tail->next = NULL;
}
}
--this->size;
}