题目

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (<105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [−105,105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

易错点

  1. 测试点4:考查的是对脏数据的处理,即给出的首地址在链表各个结点当中的首地址中找不到时的输出,如测试样例:

    3 -1
    54322 2 -1
    09876 90 12345
    12345 9 09868
    

    输出应当为0 -1,前面的0表示该链表没有任何结点,-1表示首地址为空。

  2. 可能存在给出的结点不在链表当中的情况,因此需要判断每一个结点是否链表当中,然后对在链表当中的元素进行排序。

代码

#include<bits/stdc++.h>
using namespace std;
struct Node{int address;int key;int next;int in_list;//标记是否在链表当中
};struct Node N[100000];//<10^5,里面的int会自动初始化为0int cmp(const void *a, const void *b){struct Node c = *(struct Node *)a;struct Node d = *(struct Node *)b;if (c.in_list==d.in_list)return c.key - d.key;//升序elsereturn d.in_list - c.in_list;//降序,在链表中的排列在前面
}int main(){int n,s,i,t1,t2,t3,y=0,num=0;//num记录链表中的结点的实际个数scanf("%d %d",&n,&s);for (i=0;i<n;i++){scanf("%d %d %d",&t1,&t2,&t3);N[t1].address = t1;N[t1].key = t2;N[t1].next = t3;if (t1==s)y = 1;//说明链表是存在的(能找到链表的开始)}if (y==0)printf("0 -1");else{do{N[s].in_list = 1;//在链表中s = N[s].next;num++;}while (s!=-1);qsort(N,100000,sizeof(N[0]),cmp);printf("%d %05d\n",num,N[0].address);for (i=0;i<num;i++){if (i!=(num-1))printf("%05d %d %05d\n",N[i].address,N[i].key,N[i+1].address);elseprintf("%05d %d -1",N[i].address,N[i].key);}}return 0;
}

PAT甲级1052:Linked List Sorting (25)相关推荐

  1. PAT甲级1052 Linked List Sorting:[C++题解]链表排序

    文章目录 题目分析 题目链接 题目分析 题意:给定数据(里面有不构成链表的数据,若是,则跳过),是链表的构成链表.然后根据数值大小重新排序,构成新的链表. 分析:用数组模拟链表,先建立链表.遍历链表, ...

  2. 1052 Linked List Sorting (25 分)

    Sample Input: 5 00001 11111 100 -1 00001 0 22222 33333 100000 11111 12345 -1 33333 22222 1000 12345 ...

  3. 【附段错误原因,最后两个测试点】1052 Linked List Sorting (25 分)【链表类题目总结】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A linked list consists of a series of structures, which are not n ...

  4. 1052. Linked List Sorting (25)

    考察链表的知识,以及排序 题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1052 #include<iostream> #inclu ...

  5. 【PAT甲级 - 1028】List Sorting (25分)(模拟,排序)

    题干: Excel can sort records according to any column. Now you are supposed to imitate this function. I ...

  6. 1052 Linked List Sorting (25 分)【难度: 一般 / 知识点: 链表】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 哈希表模拟链表. #include<b ...

  7. 1052 Linked List Sorting (25分)

    和 1074一样的方法,只需要管自己的结点就可以了,不需要处理next, 注意最后一个测试点,测试数据最终结果 为0个结点,只需要输出 0 -1 附本人AC代码: #include<iostre ...

  8. 1052. Linked List Sorting

    没仔细审题,走了很多弯路.题目要求:内存中有很多散列的节点,可能构成不止一个链表.其次要注意空链表的情况. // 1052. Linked List Sorting.cpp: 主项目文件.#inclu ...

  9. PAT甲级1009 Product of Polynomials (25分)

    PAT甲级1009 Product of Polynomials (25分) 题目: 题目分析:和之前有一道多项式相加很像,注意点是不仅仅数组系数会变,还可能会出现之前没有的指数,因此要开2001大小 ...

  10. 1052 Linked List Sorting(排序)

    1052 Linked List Sorting(排序) 思路: s t r u c t + struct+ struct+排序. 坑点: 1.答案只包含从原链表中头结点开始的结点,有点结点不在原链表 ...

最新文章

  1. 【剑指offer-Java版】01为了准备面试也为了提升编程技巧开始刷宝典了
  2. LAMP 系统性能调优:第2 部分: 优化Apache 和PHP-学习笔记
  3. CG CTF CRYPTO easy!
  4. Log4J入门教程(二) 参数讲解
  5. 让人难以置信的HTML5和JavaScript实验
  6. 阿里云安全肖力:云的六大安全基因助力企业构建智能化安全体系
  7. IE6.0中getElementsByName和getElementById的bug
  8. 1043 Is It a Binary Search Tree (25 分) BST反转?不反转 遍历+vector
  9. 数据分析 第七篇:方差分析(单因素方差分析)
  10. 做鼻子测试软件,美鼻小测试,测测你的鼻子有几分?
  11. Android获取WiFi名称/路由器AP地址总结
  12. C语言中的int类型的范围是由什么决定的
  13. 前端-table表格隔行变色
  14. 汇编语言:AX、BX、CX、DX寄存器知识点梳理
  15. 关于STM32与OpenMv通讯踩过的那些坑(1)
  16. android批量上传图片(模仿QQ空间和微信发表说说)
  17. input禁止自动填充
  18. 个税起征点升高----节省了多少?
  19. 从爬取豆瓣影评到基于朴素贝叶斯的电影评论情感分析(上)
  20. DSP PWM 模块原理及使用

热门文章

  1. 华龙集团为您介绍78629116壁纸生产设备
  2. Apple将在2013年启动香港数据中心的建设
  3. Android基础—深入了解AccessibilityService
  4. 异常检测主要方法总结
  5. 各大电商平台API常用方法总结(必看篇)
  6. 手把手教你创建专属个人助理,GitHub免费的
  7. python网络安全论文题目_计算机科学与技术专业毕业论文参考题目.doc
  8. 教改论文 计算机,容易写的计算机教改论文题目 计算机教改专业论文题目如何拟...
  9. scp linux 命令加密码,scp命令_Linux scp 命令用法详解:加密的方式在本地主机和远程主机之间复制文件...
  10. 常用计算机程序英语,计算机程序编程中的常用英语