https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464
/* *链表的处理 *1.定义静态链表,结构体数组 *2.初始化falg为false *3.从链表首地址begin遍历,并标记有效结点 *4.对结点排序,有效结点true大于false */ #include<iostream> using namespace std; #include<cstdio> #include<algorithm> const int MAXN=100010; struct Node { int address,data,next; bool falg; }node[MAXN]; bool cmp(Node a,Node b) { //函数cmp为bool类型,参数为Node if(a.falg==false || b.falg==false) { return a.falg>b.falg; //把无效结点往后放 }else { return a.data<b.data; } } int main() { for(int i=0;i<MAXN;i++) { node[i].falg=false; } int num,begins,address; scanf("%d%d",&num,&begins); for(int i=0;i<num;i++) { scanf("%d",&address); //先读入地址 scanf("%d%d",&node[address].data,&node[address].next); node[address].address=address; } int counts=0,point=begins; while(point != -1) { node[point].falg=true; counts++; point=node[point].next; } if(counts==0) { //特判,新链表没有结点时输出0 -1 printf("0 -1"); }else { sort(node,node+MAXN,cmp); printf("%d %05d\n",counts,node[0].address); for(int i=0;i<counts;i++) { if(i != counts-1) { //结点地址node[i],node[i+1] printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address); }else { printf("%05d %d -1\n",node[i].address,node[i].data); } } } return 0; }