-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_A1052.cpp
More file actions
116 lines (106 loc) · 2.66 KB
/
7_A1052.cpp
File metadata and controls
116 lines (106 loc) · 2.66 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
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
/**
* P265 Linked List Sorting
*/
#include <iostream>
#include <iomanip>
#include <algorithm>
#define MAXN 100010
using namespace std;
class Node
{
public:
int address;
int data;
int next;
// 节点是否在链表上
bool flag = false;
Node() {}
};
/**
* 返回有效节点数
*/
int listTraverse(Node staticList[], int startAddr)
{
int nodeCount = 0;
for (int i = startAddr; i != -1; i = staticList[i].next)
{
staticList[i].flag = true;
nodeCount += 1;
}
return nodeCount;
}
// 比较函数
// 如果 flag == true,说明是链表中的节点,根据 data 从小到大排序
// 如果 flag == false,说明不是链表中的节点,往数组后面放,也就是按 flag 从大到小排序
bool cmp(Node a, Node b)
{
if (a.flag == true && b.flag == true)
{
return a.data < b.data;
}
else
{
return a.flag > b.flag;
}
}
void listSort(Node staticList[], int startAddr, int nodeCount)
{
// 进行排序
// 效果:flag 为 true(1) 的在 staticList 的左端,且按照 data 从小到大进行排序
sort(staticList, staticList + MAXN, cmp);
// traverse and update the new 'next'
for (int i = 0; i < nodeCount; i++)
{
staticList[i].next = staticList[i].address;
// 最后一个有效节点
if (i == nodeCount - 1)
{
staticList[i].next = -1;
}
}
}
void print(Node staticList[], int nodeCount)
{
cout << "After having sorted the list: " << endl;
cout << "Node count: " << nodeCount << endl;
for (int i = 0; i < nodeCount; i++)
{
cout << "<" << setw(5) << setfill('0') << staticList[i].address << ", ";
cout << staticList[i].data << ", ";
if (staticList[i].next == -1)
{
cout << -1 << ">" << endl;
return;
}
cout << setw(5) << setfill('0') << staticList[i].next << ">" << endl;
}
}
int main()
{
// 声明静态链表
Node staticList[MAXN];
// input
int startAddr;
int totalNum;
cout << "Input total number and start address: ";
cin >> totalNum >> startAddr;
cout << "Input info as <addr letter next>: " << endl;
for (int i = 0; i < totalNum; i++)
{
int addr;
int data;
int next;
cin >> addr >> data >> next;
staticList[addr].address = addr;
staticList[addr].data = data;
staticList[addr].next = next;
staticList[addr].flag = false;
}
// traverse
int nodeCount = listTraverse(staticList, startAddr);
// sort
listSort(staticList, startAddr, nodeCount);
// print
print(staticList, nodeCount);
return 0;
}