Linked List Sorting (链表)

 

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、可能是空链表  2、并不是每个节点都在链表上的



#include <iostream>

#include <vector>

#include<iomanip>

#include <algorithm>

using namespace std;

struct node

{

  int add;

  int data;

  int next;

};

node a[100000],b[100000];

bool cmp(node n1,node n2)

{

   return n1.data<n2.data;

}

int main()

{

      int len,fir,add,data,next;

      int i;

      while(cin>>len)

      {

            cin>>fir;

            for(i=0;i<len;i++)

            {

               cin>>add>>data>>next;

               node n;

               n.add=add;

               n.data=data;

               n.next=next;

               a[add]=n;

            }

            if(fir==-1)

            {

              cout<<"0 -1"<<endl;

              continue;

            }

        i=0;

            while(fir!=-1)

            {

               b[i].add =a[fir].add;

               b[i].data =a[fir].data;

               b[i].next =a[fir].next;

               fir = a[fir].next;

               ++i;

            }

          int len=i;

         sort(b,b+len,cmp);

            for(i=0;i<len-1;i++)

            {

                b[i].next=b[i+1].add;

            }

            b[len-1].next=-1;

            cout<<len<<" "<<setfill('0')<<setw(5)<<b[0].add<<endl;

            for(i=0;i<len;i++)

            {

                 if(b[i].next==-1)

                        cout<<setfill('0')<<setw(5)<<b[i].add<<" "<<b[i].data<<" "<<-1<<endl;

                  else

                        cout<<setfill('0')<<setw(5)<<b[i].add<<" "<<b[i].data<<" "<<setfill('0')<<setw(5)<<b[i].next<<endl;

            }

      }

  return 0;

}

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 ne ...

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

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

  3. PAT1052:Linked List Sorting

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

  4. PAT 1052 Linked List Sorting [一般]

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

  5. Pat 1052 Linked List Sorting (25)

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

  6. pat1052. Linked List Sorting (25)

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

  7. Linked List Sorting

    静态链表(用结构体数组模拟链表)     1052 Linked List Sorting (25分)   A linked list consists of a series of structur ...

  8. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  9. 【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 ...

随机推荐

  1. WdatePicker时间控件联动选择

    $("#txtStartTime").bind("click focus", function () { var endtimeTf = $dp.$('txtE ...

  2. thymleaf分支用法

    <div th:switch="${user.role}"> <p th:case="'admin'">User is an admin ...

  3. Centos6.4安装Mono和MonoDevelop

    Mono官方网站:http://www.mono-project.com MonoDevelop官方网站:http://monodevelop.com/ 注:整个安装过程最好在同一个终端下完成!   ...

  4. Linux下配置Node环境变量及问题详解

    这是之前在Linux下配置Node环境变量时踩过的坑,今天又有小伙伴询问这个问题,因此记录下来,不仅是给新童鞋们一些参考,也方便日后查阅 在这之前,相信都已经安装好了,没安装的可以查看博主另一篇文章 ...

  5. linux安装

    1.rpm: • RPM是软件包管理工具,是Redhat Package Manager的缩写,最早由redhat公司引入的,现在已经成为公认的行业标准了.• 什么是package• 简单地说就是归档 ...

  6. TortoiseGit无法勾选Load Putty Key,该选项为灰色的解决办法

    1.软件版本 Git版本:Git-1.9.0-preview20140217.exe TortoiseGit:TortoiseGit-1.8.8.0-64bit.msi     2.出现问题 使用右键 ...

  7. Java计算文件的SHA码和MD5码

    可参考:http://blog.csdn.net/hudashi/article/details/8394158 /** * 计算文件的MD5码 * @param file * @return */ ...

  8. 关于String和StringBuffer的使用

    java本身提供了n多对象方便我们进行开发,这些对象都是遵从着面向对象思想而出现的,后期再讲就不再讲思想了,而是不断学习这些对象的使用.我们的学习重点已经开始从面向对象思想转移到了面向对象的使用上来了 ...

  9. 前端开发中的一些chrome插件推荐

    这篇博客推荐的都是谷歌chrome浏览器插件,理论上,与之相同内核的浏览器都能使用.由于是谷歌插件,所以在天朝的网络,你懂的! 红杏 专为 学者 .程序员.外贸工作者 打造的上网加速器.我们相信,上网 ...

  10. 开始迈入Csharp的世界了

    以前没有学过, 有点兴奋,完全是新手啊. 今天是元宵情人节, 加油↖(^ω^)↗ 分享一些话:   一则拉封丹寓言名字叫寓言<两个朋友>:    一天夜里,一个朋友突然去找另一个朋友.被吵 ...