forked from Ewenwan/ShiYanLou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genDLList.h
125 lines (114 loc) · 2.95 KB
/
genDLList.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
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
#ifndef DOUBLY_LINKED_LIST
#define DOUBLY_LINKED_LIST
template<class T>
// 双向链表 节点 包含信息 和 两个地址指针 分别指向 前驱节点 和 后继节点
class DLLNode {
public:
DLLNode() {
next = prev = 0;
}
DLLNode(const T& el, DLLNode<T> *n = 0, DLLNode<T> *p = 0) {
info = el; next = n; prev = p;
}
T info;// 包含信息
DLLNode<T> *next, *prev;// 两个地址指针 分别指向 前驱节点 和 后继节点
};
template<class T>
class DoublyLinkedList {
public:
// 默认构造函数
DoublyLinkedList() {
head = tail = 0;
}
// 默认析构函数
void addToDLLTail(const T&);
T deleteFromDLLTail();
~DoublyLinkedList() {
clear();
}
bool isEmpty() const {
return head == 0;
}
void clear();
void setToNull() {
head = tail = 0;
}
void addToDLLHead(const T&);
T deleteFromDLLHead();
T& firstEl();
T* find(const T&) const;
protected:
DLLNode<T> *head, *tail;
friend ostream& operator<<(ostream& out, const DoublyLinkedList<T>& dll) {
for (DLLNode<T> *tmp = dll.head; tmp != 0; tmp = tmp->next)
out << tmp->info << ' ';
return out;
}
};
// 头部添加
template<class T>
void DoublyLinkedList<T>::addToDLLHead(const T& el) {
if (head != 0) {
head = new DLLNode<T>(el,head,0);
head->next->prev = head;
}
else head = tail = new DLLNode<T>(el);
}
template<class T>
void DoublyLinkedList<T>::addToDLLTail(const T& el) {
if (tail != 0) {
tail = new DLLNode<T>(el,0,tail);
tail->prev->next = tail;
}
else head = tail = new DLLNode<T>(el);
}
template<class T>
T DoublyLinkedList<T>::deleteFromDLLHead() {
T el = head->info;
if (head == tail) { // if only one DLLNode on the list;
delete head;
head = tail = 0;
}
else { // if more than one DLLNode in the list;
head = head->next;
delete head->prev;
head->prev = 0;
}
return el;
}
template<class T>
T DoublyLinkedList<T>::deleteFromDLLTail() {
T el = tail->info;
if (head == tail) { // if only one DLLNode on the list;
delete head;
head = tail = 0;
}
else { // if more than one DLLNode in the list;
tail = tail->prev;
delete tail->next;
tail->next = 0;
}
return el;
}
template <class T>
T* DoublyLinkedList<T>::find(const T& el) const {
DLLNode<T> *tmp = head;
for ( ; tmp != 0 && !(tmp->info == el); // overloaded ==
tmp = tmp->next);
if (tmp == 0)
return 0;
else return &tmp->info;
}
template<class T>
T& DoublyLinkedList<T>::firstEl() {
return head->info;
}
template<class T>
void DoublyLinkedList<T>::clear() {
for (DLLNode<T> *tmp; head != 0; ) {
tmp = head;
head = head->next;
delete tmp;
}
}
#endif