-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathDLList.h
More file actions
77 lines (66 loc) · 1.59 KB
/
DLList.h
File metadata and controls
77 lines (66 loc) · 1.59 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
#ifndef __JDLLIST__
#define __JDLLIST__
#include <stdio.h>
namespace OrderBook {
//-----------------------------------------------------------------------------
template <typename NODE>
class DLList {
public:
DLList()
: head_(0)
, tail_(0)
{}
virtual ~DLList() { }
void addNode(NODE *input) {
if (head_ == 0) {
head_ = input;
tail_ = head_;
return;
}
head_->next_ = input;
input->previous_ = head_;
head_ = input;
}
void removeNode(NODE *target) {
// Skip self and delete if straddled
if (target->previous_ != 0 && target->next_ != 0) {
target->previous_->next_ = target->next_;
target->next_->previous_ = target->previous_;
}
// Edge - tail deleted
else if (target == tail_ && target->next_ != 0) {
tail_ = target->next_;
tail_->previous_ = 0;
}
// Edge - head deleted
else if (target == head_ && target->previous_ != 0) {
head_ = target->previous_;
head_->next_ = 0;
}
// Edge - single element list
else {
head_ = 0;
tail_ = 0;
return;
}
}
NODE *getHead() const { return head_; }
NODE *getTail() const { return tail_; }
#ifdef DEBUG
void printList() {
NODE *head = tail_->previous_;
while (head != tail_) {
fprintf(stderr, "NODE: ");
head->printSelf();
head = head->previous_;
}
fprintf(stderr, "TAIL NODE: ");
head->printSelf();
}
#endif
private:
NODE *head_;
NODE *tail_;
};
} // namespace
#endif