题意:链表排序

思路:题目本身并不难,但是这题仔细读题很重要。原题中有一句话,"For each case, the first line contains a positive N and an address of the head node, where N is the total number of nodes in memory",它没有明确表示这个N就是链表的结点个数,而是内存中含有的结点个数(大家可以相比较另外几道链表的题目,看看题目描述的细微差别)。因此,在读入数据后,要从头结点开始遍历一遍链表,把链表上的结点提取出来,因为,输入的链表有可能是不属于这个链表的干扰结点。有几个坑需要注意——

1.输入的结点并不全是链表的结点,因此在正式排序前应先提取出有效结点;

2.有可能在第1步完成后,有效结点为0,故输出 0 -1。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;

;
struct Node{
    int data;
    int curr,next;
}LinkList[N],node[N];

bool cmp(Node a,Node b){
    return a.data<b.data;
}

int main()
{
    int n,head;
    scanf("%d%d",&n,&head);
    int curr,data,next;
    ;i<n;i++){
        scanf("%d%d%d",&curr,&data,&next);
        LinkList[curr].data=data;
        LinkList[curr].curr=curr;
        LinkList[curr].next=next;
    }  //提取有效结点
    ;
    ){
        node[cnt++]=LinkList[head];
        head=LinkList[head].next;
    }
    sort(node,node+cnt,cmp);
    ){//空链表的情况
        printf("0 -1\n");
        ;
    }
    printf(].curr);
    ;i<cnt;i++){//注意遍历个数cnt,不是n
        printf("%05d %d ",node[i].curr,node[i].data);
        ) printf(].curr);
        else printf("-1\n");
    }
    ;
}

常见的错误写法:

#include <cstdio>
#include <algorithm>
using namespace std;
;
struct Node{
    int data;
    int curr,next;
}LinkList[N];

bool cmp(Node a,Node b){
    return a.data<b.data;
}

int main()
{
    //freopen("pat.txt","r",stdin);
    int n,head;
    scanf("%d%d",&n,&head);
    int curr,key,next;
    ;i<n;i++){
        scanf("%d%d%d",&curr,&key,&next);
        LinkList[i].data=key;
        LinkList[i].curr=curr;
        LinkList[i].next=next;
    }
    sort(LinkList,LinkList+n,cmp);
    printf(].curr);
    ;i<n;i++){
        printf("%05d %d ",LinkList[i].curr,LinkList[i].data);
        ) printf(].curr);
        else printf("-1\n");
    }
    ;
}

1052 Linked List Sorting的更多相关文章

  1. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  2. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  3. PAT 1052 Linked List Sorting [一般]

    1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...

  4. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  5. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  6. 1052. Linked List Sorting (25)

    题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...

  7. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

  8. PAT Advanced 1052 Linked List Sorting (25) [链表]

    题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...

  9. PAT (Advanced Level) 1052. Linked List Sorting (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  10. PAT甲级1052 Linked List Sorting

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...

随机推荐

  1. Dynamo概述

    Dynamo 是Amazon提供的一款高可用的分布式Key-Value存储系统,其满足可伸缩性.可用性.可靠性. CAP原理满足:通过一致性哈希满足P,用复制满足A,用对象版本与向量时钟满足C.用牺牲 ...

  2. NFS的安装配置使用

    /////////////////////////////NFS///////////////////////////////////////////////////写在前面:NFS在数据传输/信息传 ...

  3. angularjs地址栏传参

    1:路由定义参数 2.controller 3. 4.目标得到参数值

  4. 新东方雅思词汇---6.3、brilli

    新东方雅思词汇---6.3.brilli 一.总结 一句话总结: 发光 brilliant 英 [ˈbrɪlɪənt]  美 ['brɪljənt]  adj. 灿烂的,闪耀的:杰出的:有才气的:精彩 ...

  5. 【疯狂labview】 Xcontrol+LVoop封装练习 Toolbar

    labview没有toolbar,并且没有可复用的 控件 其实项目中很需要 特别是在框架平台的搭建上, 本文以Xcontrol用PICTURE的方式实现toolbar,并用LVoop封装 废话少说 直 ...

  6. eclipse indigo 安装 Eclipse Marketplace Client

    打开 eclipse,help--Eclipse Marketplace Client就能找到 有的eclipse中没有这个功能就需手动添加Eclipse Marketplace Client. he ...

  7. centos 使用yum安装MySQL 5.7

    想在centos上安装一个MySQL,使用yum install mysql-server 安装提示仓库没有包,也是醉了. 找了很多博客,发现一个很好用的,推荐给大家. 地址:https://blog ...

  8. 014对象——对象 __isset __unset __sleep __wakeup

    <?php /** * */ /*class lantian { public $name; public $age; private $money; public $c; function _ ...

  9. MVC框架中的值提供机制(二)

    在MVC框架中存在一些默认的值提供程序模板,这些值提供程序都是通过工厂模式类创建;在MVC框架中存在需要已Factory结尾的工厂类,在值提供程序中也存在ValueProviderFactories工 ...

  10. 在 Ubuntu 上搭建 Hadoop 分布式集群 Eclipse 开发环境

    一直在忙Android FrameWork,终于闲了一点,利用空余时间研究了一下Hadoop,并且在自己和同事的电脑上搭建了分布式集群,现在更新一下blog,分享自己的成果. 一 .环境 1.操作系统 ...