给出n个节点,把链表中的节点按照key值从小到大排列。
`
c++
#include
#include
using namespace std;
struct NODE {
int address, key, next;
bool flag;
}node[100000];
int cmp1(NODE a, NODE b) {
return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
// !a.flag || !b.flag为真即有节点不在链表里,往后放a.flag > b.flag,真1在前,假0在后
// 然后按照递增排列
}
int main() {
int n, cnt = 0, s, a, b, c;
scanf(“%d%d”, &n, &s);
for(int i = 0; i < n; i++) {
scanf(“%d%d%d”, &a, &b, &c);
node[a] = {a, b, c, false};
}
// 输入数据n个节点,s为root
for(int i = s; i != -1; i = node[i].next) {
node[i].flag = true;
cnt++;
}
// 链表中的节点标为true,因为可能节点并不存在于链上
if(cnt == 0) {
printf(“0 -1”);
} else {
sort(node, node + 100000, cmp1);
printf(“%d %05d\n”, cnt, node[0].address);
for(int i = 0; i < cnt; i++) {
printf(“%05d %d “, node[i].address, node[i].key);
if(i != cnt - 1)
printf(“%05d\n”, node[i + 1].address);
else
printf(“-1\n”);
}
}
return 0;
}