1052 Linked List Sorting (25 分)

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 (<10​5​​) 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 [−10​5​​,10​5​​], 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

题目大意:给出一个链表,按照其中存储的key值大小重新排序。

最终AC:

#include <iostream>
#include <algorithm>
#include<cstdio>
#include <vector>
using namespace std; struct Node{
int now,num,next;
}node[];
vector<Node> vt;
bool cmp(Node& a,Node& b){
return a.num<b.num;
}
int main()
{
int n,f;
scanf("%d%d",&n,&f);
int t,num,next;
for(int i=;i<n;i++){
cin>>t>>num>>next;
node[t].num=num;
node[t].next=next;
node[t].now=t;
}
for(int index=f;index!=-;index=node[index].next){
vt.push_back(node[index]);
} //有一个疑问就是如果输入数据为0 0 ,怎么输出?
if(vt.size()==){
printf("0 -1");
}else{
sort(vt.begin(),vt.end(),cmp);
printf("%d %05d\n",vt.size(),vt[].now);
for(int i=;i<vt.size();i++){
if(i!=vt.size()-){
printf("%05d %d %05d\n",vt[i].now,vt[i].num,vt[i+].now);
}else{
printf("%05d %d -1",vt[i].now,vt[i].num);
}
}
}
return ;
}

//自己遇到了很多坑。。。

1.对于输入是0 0不知道如何输出,看了别人的发现是输出0和-1,记住吧,以后对于链表长度为0的,就是输出数量0,然后直接就是结束-1.

2.第一次提交时有段错误,感觉到是数组定义长度的问题,一开始定义长度为100000,后来又加了10可以了,最后一个测试点,那么以后在定义时就比平常多加10就可以啦

3.之后提交发现第4个测试点过不去,这是为什么呢?去牛客网上发现是如下错误:

测试用例:

  -

对应输出应该为:

  -

你的输出为:

最终在输出起点坐标时加上了%05d,就可以了。。。。

最新文章

  1. Emit学习(1) - HelloWorld
  2. 通过set和waitOne来控制子线程的运行和停止
  3. POJ 1417 True Liars(种类并查集+dp背包问题)
  4. ORACLE RAC 监听配置 (listener.ora tnsnames.ora)
  5. cocos2d-x3.9 NDK android 环境搭建过程中遇到的错误
  6. mysql的查询缓存模式介绍
  7. 读《你不知道的JavaScript(上卷)》后感-浅谈JavaScript作用域(一)
  8. 如何通过setTimeout理解JS运行机制详解
  9. Java 快速排序法 冒泡排序法 选择排序法 插入排序法
  10. Django学习手册 - cookie / session
  11. C#检查服务状态和启动关闭服务
  12. RequestDispatcher.forward转发与HttpServletResponse.sendRedirect重定向
  13. *2.3.3-加入monitor
  14. yii学习笔记(4),获取请求数据的request组件
  15. A Translation for Quaternion 一篇对四元数的翻译
  16. Java—集合框架Map
  17. HBase配置和使用
  18. 小计---pandas读取带有中文文件名或者包含中文内容的文件
  19. git学习--远程分支删除
  20. web worker,SSE,WebSocket,AJAX 与后端交互的方式

热门文章

  1. php插入代码数据库
  2. Collection接口都是通过Iterator()(即迭代器)来对Set和List遍历
  3. erlang的汉字字符串和二进制的相互转换,并还原成汉字打印
  4. bootstrap基础学习八篇
  5. hdu 1253:胜利大逃亡(基础广搜BFS)
  6. jQuery实现瀑布流布局详解(PC和移动端)
  7. iOS 功能代码 上传到 远程 码云私有库
  8. js实现二分搜索法
  9. Java Tomcat 注册为Windows系统服务
  10. HashMap实现原理、核心概念、关键问题的总结