PAT 1052. Linked List Sorting (25)

这道题和1074差不多,在一个链表上进行排序
1.首先将在一个链表的分离出来
2.排序
3**注意判断输入为0和输入head不存在的情况**

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;

struct p
{
  int add;
  int Key;
  int next;
}node[100004];

bool cmp(p x,p y)
{
  return x.Key < y.Key;
}
int main()
{
  int n,head;
  while(scanf("%d%d",&n,&head) != EOF)
  {
    p ListNode[100004];
    int cnt = 0;
    for(int i = 0; i < n; i++)
    {
    int x;
    scanf("%d",&x);
    node[x].add = x;
    scanf("%d%d",&node[x].Key,&node[x].next);
  }
  while(head != -1)//将在一个链表的节点取出
  {
    if(head == node[head].add)
    {
      ListNode[cnt++] = node[head];
      head = node[head].next;
    }else
      break;
  }
  if(cnt == 0)//判断0的情况
  {
    printf("0 -1\n");
    continue;
  }
  sort(ListNode,ListNode+cnt,cmp);//排序
  printf("%d %05d\n",cnt,ListNode[0].add);
    for(int i = 0; i < cnt-1; i++)
    printf("%05d %d %05d\n",ListNode[i].add,ListNode[i].Key,ListNode[i+1].add);
  printf("%05d %d -1\n",ListNode[cnt-1].add,ListNode[cnt-1].Key);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值