链表

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;

template <typename T>
struct listNode {
T data;
listNode<T>*succ, *pred; // successor, predecessor
public:
listNode() { succ = nullptr, pred = nullptr; }
listNode(T data, listNode<T>* s = nullptr, listNode<T>* p = nullptr)
: data(data), succ(s), pred(p) {}

listNode<T>* insertAsSucc(const T& rhs); // 返回插入后的位置
listNode<T>* insertAsPred(const T& rhs);
};

template <typename T>
listNode<T>* listNode<T>::insertAsSucc(const T& rhs) { // 插到该节点的后继
listNode<T>* it = new listNode<T>(rhs, succ, this);
succ->pred = it;
succ = it;
return it;
}

template <typename T>
listNode<T>* listNode<T>::insertAsPred(const T& rhs) { // 插到该节点的前驱
listNode<T>* it = new listNode<T>(rhs, this, pred);
pred->succ = it;
pred = it;
return it;
}

// --------------------------------------------------------------------------------------------------
template <typename T>
class linkList {
listNode<T>*header, *trailer; // 尾节点方便尾部插入和删除节点
uint _size;

public:
linkList();
~linkList();

T& operator[](uint p) const; // const: 该函数不会修改数据成员

listNode<T>* begin() { return header->succ; }
listNode<T>* end() { return trailer; }

listNode<T>* find(const T& data) const;
listNode<T>* insertAsFirst(const T& data) {
++_size;
return header->insertAsSucc(data);
}
listNode<T>* insertAsLast(const T& data) {
++_size;
return trailer->insertAsPred(data);
}
T remove(listNode<T>* p);

listNode<T>* search(listNode<T>* begin, listNode<T>* end,
const T& data) const; // 找到第一个大于等于data的位置

// 排序算法,左闭右开
void insertionSort(listNode<T>* begin, listNode<T>* end); // 插入排序
void selectionSort(listNode<T>* begin, listNode<T>* end); // 选择排序
listNode<T>* mergeSort(listNode<T>* begin, listNode<T>* end,
int n = -1); // 归并排序
void quickSort(listNode<T>* begin, listNode<T>* end); // 快速排序
};

template <typename T>
linkList<T>::linkList() {
header = new listNode<T>;
trailer = new listNode<T>;
header->succ = trailer;
trailer->pred = header;
_size = 0;
}

// 如果不throw,可以设置成private
template <typename T>
T& linkList<T>::operator[](uint p) const {
// if (p >= sz) throw;
listNode<T>* now = header->succ;
while (p--) now = now->succ;
return now->data;
}

template <typename T>
listNode<T>* linkList<T>::find(const T& data) const {
listNode<T>* it = header->nxt;
while (it != trailer) {
if (it->data == data)
return it;
else
it = it->nxt;
}
return nullptr;
}

template <typename T>
T linkList<T>::remove(listNode<T>* p) {
T tmp = p->data;
p->pred->succ = p->succ;
p->succ->pred = p->pred;
delete p;
--_size;
return tmp;
}

template <typename T>
linkList<T>::~linkList() {
listNode<T>* it = header;
while (it != nullptr) {
auto tmp = it->succ;
delete it;
it = tmp;
}
_size = 0;
}

template <typename T>
listNode<T>* linkList<T>::search(listNode<T>* begin, listNode<T>* end,
const T& data) const {
listNode<T>* it = begin;
while (it != end) {
if (it->data >= data) return it;
it = it->succ;
}
return end;
}
template <typename T>
void linkList<T>::insertionSort(listNode<T>* begin, listNode<T>* end) {
if (begin == end) return;
listNode<T>* head = begin;
listNode<T>* it = begin->succ;
while (it != end) {
listNode<T>* p =
search(head, it, it->data); // 找到第一个大于等于data的位置
p->insertAsPred(it->data); // 插入到它的前驱
if (p == head) head = p->pred;
listNode<T>* succ = it->succ;
remove(it);
it = succ;
}
}

template <typename T>
void linkList<T>::selectionSort(listNode<T>* begin, listNode<T>* end) {
if (begin == end) return;
listNode<T>* it = begin;
while (it != end) {
listNode<T>*p = it->succ, *Min = it;
while (p != end) {
if (p->data < Min->data) Min = p;
p = p->succ;
}
if (it == Min)
it = it->succ;
else
it->insertAsPred(Min->data), remove(Min);
}
}

template <typename T>
listNode<T>* linkList<T>::mergeSort(listNode<T>* begin, listNode<T>* end,
int n) { // 默认参数写在定义中
if (n == -1) { // 计算长度
n = 0;
listNode<T>* it = begin;
while (it != end) ++n, it = it->succ;
}
if (begin == end || n < 2) return begin;

int mid = n / 2;
listNode<T>* midNode = begin;
for (int i = 0; i < mid; i++) midNode = midNode->succ;
begin = mergeSort(begin, midNode, mid);
midNode = mergeSort(midNode, end, n - mid);
// 因为有删除操作,因此merge后首节点可能会改变。所以函数要返回排序后的首节点。

listNode<T>*l1 = begin, *l2 = midNode;

// 注意判断条件, 因为有删除操作,所以不能判 l1 != minNode, 而要判 l1 != l2。
while (l1 != l2 && l2 != end) {
if (l1->data <= l2->data) {
l1 = l1->succ;
} else {
l1->insertAsPred(l2->data);
if (l1 == begin) begin = l1->pred;
l2 = l2->succ;
remove(l2->pred);
}
}
return begin;
}

template <typename T>
void linkList<T>::quickSort(listNode<T>* begin, listNode<T>* end) {
if (begin == end || begin->succ == end) return; // 没有元素或只有一个元素

bool isSorted = true; // 检查是否已经排好序,特判序列元素全相等的情况。
listNode<T>* p = begin->succ;
while (p != end) {
if (p->data < p->pred->data) {
isSorted = false;
break;
}
p = p->succ;
}
if (isSorted) return;

listNode<T>* mid = begin; // 选第一个节点作为分界点
p = begin->succ;
while (p != end) {
if (p->data < mid->data) {
mid->insertAsPred(p->data);
if (mid == begin) begin = mid->pred; // 首节点会改变
p = p->succ;
remove(p->pred);
} else
p = p->succ;
}
quickSort(begin, mid);
quickSort(mid->succ, end);
}

int main() {
linkList<int> test;
mt19937 rnd(time(0));
int n = 10;
for (int i = 0; i < n; i++) test.insertAsLast(rnd());
for (int i = 0; i < n; i++) cout << test[i] << ' ';
cout << endl;

test.quickSort(test.begin(), test.end());
for (int i = 0; i < n; i++) cout << test[i] << ' ';
cout << endl;

test.mergeSort(test.begin(), test.end());
for (int i = 0; i < n; i++) cout << test[i] << ' ';
cout << endl;

test.selectionSort(test.begin(), test.end());
for (int i = 0; i < n; i++) cout << test[i] << ' ';
cout << endl;
}