1052 Linked List Sorting (25)

#include<cstdio>
#include<algorithm>
#define maxn 100005

using namespace std;

struct Node{
   
	int address,data,next,inList;
}node[maxn];

bool cmp(Node a, Node b){
   
	if(a.inList==false||b.inList==false){
   
		return a.inList>b.inList;
	}else{
   
		return a.data<b.data;
	}
}
int main(){
   
	int n,head,address,data,next,count=0;
	scanf("%d%d",&n,&head);
	for(int i=0;i<n;i++){
   
		scanf("%d%d%d",&address,&data,&next);
		node[address]={
   address,data,next,0};
	}
	for(int i=head;i!=-1;i=node[i].next)
	{
   
		node[i].inList=1;
		count++;
	}
	if(!count) printf("0 -1\n");
	else{
   
		sort(node,node+maxn,cmp);
		printf("%d %05d\n",count,node[0].address);
		for(int i=0;i<count-1;i++){
   
			printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
		}
		printf("%05d %d -1\n",node[count-1].address,node[count-1].data);
	}
	return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-26 14:50
人力小鱼姐:有后面墨迹那两句的时间问题早回答完了
点赞 评论 收藏
分享
06-26 19:47
中南大学 Java
悲,毕业了!这是个坏事儿啊!
爱睡觉的冰箱哥:《这是个好事啊》---峰哥浪走天涯
毕业后不工作的日子里我在...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务