题意:链表排序

思路:题目本身并不难,但是这题仔细读题很重要。原题中有一句话,"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. scjp考试准备 - 11 - 类型转换2

    题目如下: interface Foo{} class Alpha implements Foo{} class Beta extends Alpha{} public class Delta ext ...

  2. java.util.logging.Logger_01

    1.参考网址 1.1.java.util.logging.Logger使用详解 http://lavasoft.blog.51cto.com/62575/184492 1.2.Java内置Logger ...

  3. GBDT 总结文档

    在做阿里的o2o优惠券预测的时候学习了GBDT.听闻GBDT的威力,自然要学习学习. 接下来从以下几个方面记录下我对于GBDT的理解. GBDT的用途,优势 GBDT的结构和算法流程 GBDT如何训练 ...

  4. 51nod 1119 组合数,逆元

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:13 ...

  5. mysql 字段属性 与 排序

    mysql中常见的数据类型:varchar(n).float.int(n).bigint(n).date.datetime.text 字段属性 默认值:DEFAULT '默认值' 非空:NOT NUL ...

  6. Caffe初试

    1.基本概念 Caffe是一个比较流行的神经网络框架,它支持C++.Python等语言,容易上手,但是代码貌似不怎么好读,等有空我...;) 2.Windows10下的部署 我把我Windows下的编 ...

  7. 软工作业-四则运算(java实现)BY叶湖倩,叶钰羽

    四则运算生成器 BY-信安1班 叶湖倩(3216005170) 信安1班 叶钰羽(3216005171) 1. 项目介绍 源代码GitHub地址:https://github.com/yeyuyu/s ...

  8. MySQL 5.7.18 在centos下安装记录

    一个朋友找我如何在linux下安装mysql5.7.18,我稍微整理下了下记录,如下: 下载地址: MySQL5.7.18参数官方网址:https://dev.mysql.com/doc/refman ...

  9. CentOS7 编译安装golang和rpm安装golang

    编译安装 1.下载golang二进制安装包: https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz 2.解压安装包到指定目录,此 ...

  10. MFC中控制Tips的显示 - lingyun1120

    from:  http://www.cnblogs.com/lingyun1120/archive/2011/11/09/2243279.html MFC中为ToolTips生成了一个类CToolTi ...