题意:链表排序

思路:题目本身并不难,但是这题仔细读题很重要。原题中有一句话,"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. elsevier 与 springer 投稿区别

    http://emuch.net/bbs/viewthread.php?tid=5369913

  2. 关于jQuery中的offset()和position()

    在jQuery中有两个获取元素位置的方法offset()和position().position()方法是在1.2.6版本之后加入的,为什么要引 入这个方法呢?这两个方法之间有什么异同?使用的时候应该 ...

  3. js 验证 -身份证等

    js验证身份证: function isIdCard(idCard) { var num = idCard.toLowerCase().match(/\w/g); if (idCard.match(/ ...

  4. 如何让vim 有颜色

    1.vi /root/.bashrc 下添加export TERM=xterm 2.同时 会话选项→仿真 勾选ANSI颜色 另外:上面如果不行,还修改了这个地方: vim ~/.vimrc synta ...

  5. UI-定时器与动画使用总结

    #pragma mark - 定时器   ******************************************************************************* ...

  6. react table dropdown

    <DropdownButton type="primary" trigger={['click']} onClick={() => this.detail(item)} ...

  7. ng 服务

    服务的本质是单例对象,封装一些方法和属性的. 单例模式:在实例化变量的时候,如果该变量已经存在,直接返回该变量:如果不存在,就创建一个新的变量再返回 ng自带的服务有很多,常用:$location $ ...

  8. BZOJ - 4771 七彩树 (可持久化线段树合并)

    题目链接 对每个结点建立两棵线段树,一棵记录该结点的子树下每种颜色对应的最小深度,另一棵记录子树下的每个深度有多少结点(每种颜色的结点只保留最浅的深度即可),自底而上令父节点继承子结点的线段树,如果合 ...

  9. vc++ windows获取计算机信息

    在软件开发中,我们经常要获当前系统的版本号,判断当前是什么系统,获取获取物理内存和可用内存大小,获取CPU名称.内核数目.主频,获取MAC地址,获取屏幕分辨率,下面的这个c++类将包含所有这些信息. ...

  10. 由于出现操作系统错误 3,进程无法读取文件D:\XXXX\X.pre (源: MSSQL_REPL,错误号: MSSQL_REPL20024)

    最近着手做SqlServer2008的订阅发布,起初使用推送订阅很顺利,后来改成请求订阅出现了以下问题,折腾好长时间终于搞定,留下此文备日后查阅,或供遇相同问题的道友参考: 首先阐述以下问题: 1. ...