-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.cpp
433 lines (406 loc) · 10.8 KB
/
address.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "address.h"
/*
链表函数
*/
AddressList::AddressList()
{
head = new Node_L;
head->next = nullptr;
}
void AddressList::init(QString filename)
{
//文件
QFile file("D:\\datastructure\\" + filename + ".txt");
if (file.exists()) qDebug() << "file exists" << endl;
else qDebug() << "file not exists" << endl;
if (file.open(QIODevice::ReadOnly)) qDebug() << "file open" << endl;
else{
qDebug() << "file open fail" << endl;
return;
}
QTextStream txt(&file);
QString line=txt.readLine();
QStringList strlist=line.split(",");
head = new Node_L;
Node_L *p=head;
head->next = nullptr;
if (strlist.size() < 3) return;
BookName = strlist[0];
len = strlist[1].toInt();
groupNum = strlist[2].toInt();
qDebug() << strlist<< endl;
line=txt.readLine();
group = line.split(",");
qDebug() << group << endl;
for (int i=0; i<len; i++)
{
QString line=txt.readLine();
QStringList strlist=line.split(",");
Node_L *temp = new Node_L;
temp->data.phone = strlist[0];
temp->data.name = strlist[1];
temp->data.email = strlist[2];
temp->data.address = strlist[3];
temp->data.remark = strlist[4];
temp->data.groupIndex = strlist[5].toInt();
p->next = temp;
p = p->next;
temp->next = nullptr;
}
file.close();
}
Node_L * AddressList::getHead()
{
return head;
}
int AddressList::getLen()
{
return len;
}
int AddressList::getGroupNum()
{
return groupNum;
}
bool AddressList::addGroup(QString groupName)
{
for (int i=0; i<groupNum; i++){
if (group[i] == groupName) return false;
}
groupNum++;
group.append(groupName);
return true;
}
void AddressList::AddressList_sort(int type, int basis)
{
//手机号排序
if(basis==1)
{
head=sortList(head);
//升序
if(type==1)
{
return ;
}
//降序
else
{
head->next=listReverse(head->next);
}
}
//姓名排序
else
{
head=sortList_name(head);
//升序
if(type==1)
{
return ;
}
//降序
else
{
head->next=listReverse(head->next);
return ;
}
}
}
//手机号
Node_L* AddressList::merge(Node_L *left, Node_L *right){
auto head = new Node_L;
auto h = head;
while(left && right){
if(left->data.phone < right->data.phone){
h->next = left;
left = left->next;
}else{
h->next = right;
right = right->next;
}
h = h->next;
}
h->next = left == NULL ? right : left;
return head->next;
}
Node_L* AddressList::merge_sort(Node_L *head){
if(!head->next) return head;
Node_L *slow = head, *fast = head->next;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
}
auto r_head = slow->next;//slow->next右边部分的头结点
slow->next = NULL;//左边切断开
auto left = merge_sort(head);
auto right = merge_sort(r_head);
return merge(left, right);
}
Node_L* AddressList::sortList(Node_L* head) {
if (!head || !head->next) return head;
return merge_sort(head);
}
//姓名
Node_L* AddressList::merge_name(Node_L *left, Node_L *right){
auto head = new Node_L;
auto h = head;
while(left && right){
if(left->data.name < right->data.name){
h->next = left;
left = left->next;
}else{
h->next = right;
right = right->next;
}
h = h->next;
}
h->next = left == NULL ? right : left;
return head->next;
}
Node_L* AddressList::merge_sort_name(Node_L *head){
if(!head->next) return head;
Node_L *slow = head, *fast = head->next;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
}
auto r_head = slow->next;//slow->next右边部分的头结点
slow->next = NULL;//左边切断开
auto left = merge_sort_name(head);
auto right = merge_sort_name(r_head);
return merge_name(left, right);
}
Node_L* AddressList::sortList_name(Node_L* head) {
if (!head || !head->next) return head;
return merge_sort_name(head);
}
Node_L* AddressList::listReverse(Node_L* head) {
Node_L* prev = nullptr;
Node_L* curr = head;
while (curr) {
Node_L* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
void AddressList::Add()
{
Node_L* nptr;
nptr=new Node_L;
nptr->data.name="unkonwn";
nptr->data.email="[email protected]";
nptr->data.phone="unknown";
nptr->data.remark="unknown";
nptr->data.address="unknown";
nptr->data.groupIndex=0;
nptr->next=nullptr;
if(head == nullptr)
{
head = nptr;
}
else
{
Node_L* pNode = head;
while(pNode->next != nullptr)
pNode = pNode->next;
pNode->next = nptr;
}
}
/*
平衡二叉树函数
*/
AddressBTree::AddressBTree()
{
root_name = nullptr;
root_email = nullptr;
}
void AddressBTree::R_Rotate(BSTree &root)
{
Node_BST *lc = root->lchild;
root->lchild = lc->rchild;
lc->rchild = root;
root = lc;
}
void AddressBTree::L_Rotate(BSTree &root)
{
Node_BST *rc = root->rchild;
root->rchild = rc->lchild;
rc->lchild = root;
root = rc;
}
void AddressBTree::L_Balance(BSTree &root)
{
Node_BST *lc = root->lchild;
switch (lc->bf) {
case 1:
root->bf = lc->bf = 0;
R_Rotate(root);
break;
case -1:
Node_BST *rd = lc->rchild;
switch (rd->bf) {
case 1: root->bf = -1; lc->bf = 0; break;
case 0: root->bf = lc->bf = 0; break;
case -1: root->bf = 0; lc->bf = 1; break;
}
rd->bf = 0;
L_Rotate(root->lchild);
R_Rotate(root);
}
}
void AddressBTree::R_Balance(BSTree &root)
{
Node_BST *rc = root->rchild;
switch (rc->bf) {
case -1:
root->bf = rc->bf = 0;
L_Rotate(root);
break;
case 1:
Node_BST *ld = rc->lchild;
switch (ld->bf) {
case 1: root->bf = 1; rc->bf = 0; break;
case 0: root->bf = rc->bf = 0; break;
case -1: root->bf = 0; rc->bf = -1; break;
}
ld->bf = 0;
R_Rotate(root->rchild);
L_Rotate(root);
}
}
bool AddressBTree::insert(BSTree &root, Data data, bool &taller, int type)
{
if (type == 1){
if (!root){
root = new Node_BST;
root->data = data;
root->lchild = root->rchild = nullptr;
root->bf = 0;
taller = true;
}
else {
if (root->data.name == data.name){
taller = false;
return false;
}
else if (data.name < root->data.name){
if (!insert(root->lchild, data, taller, type)) return false;
if (taller){
switch (root->bf) {
case 1:
L_Balance(root);
taller = false;
break;
case 0:
root->bf = 1;
taller = true;
break;
case -1:
root->bf = -1;
taller = true;
break;
}
}
}
else{
if (!insert(root->rchild, data, taller, type)) return false;
if (taller){
switch (root->bf) {
case 1:
root->bf = 0;
taller = false;
break;
case 0:
root->bf = -1;
taller = true;
break;
case -1:
R_Balance(root);
taller = false;
break;
}
}
}
}
}
else if (type == 2){
if (!root){
root = new Node_BST;
root->data = data;
root->lchild = root->rchild = nullptr;
root->bf = 0;
taller = true;
}
else {
if (root->data.email == data.email){
taller = false;
return false;
}
else if (data.email < root->data.email){
if (!insert(root->lchild, data, taller, type)) return false;
if (taller){
switch (root->bf) {
case 1:
L_Balance(root);
taller = false;
break;
case 0:
root->bf = 1;
taller = true;
break;
case -1:
root->bf = -1;
taller = true;
break;
}
}
}
else{
if (!insert(root->rchild, data, taller, type)) return false;
if (taller){
switch (root->bf) {
case 1:
root->bf = 0;
taller = false;
break;
case 0:
root->bf = -1;
taller = true;
break;
case -1:
R_Balance(root);
taller = false;
break;
}
}
}
}
}
return true;
}
BSTree AddressBTree::find(BSTree &root, int type, QString data, int &cnt)
{
if (!root) return nullptr;
if (type == 1) //按名称查找
{
cnt++;
if (data == root->data.name) return root;
else if (data < root->data.name) return find(root->lchild, type, data, cnt);
else return find(root->rchild, type, data, cnt);
}
else //按邮箱查找
{
cnt++;
if (data == root->data.email) return root;
else if (data < root->data.email) return find(root->lchild, type, data, cnt);
else return find(root->rchild, type, data, cnt);
}
}
BSTree & AddressBTree::getNameRoot()
{
return root_name;
}
BSTree & AddressBTree::getEmailRoot()
{
return root_email;
}