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. 两个数据库表同步的可视化WEB同步程序

    因业务升级,现有一个数据库中的表需要与先前项目中的表进行数据同步,停用先前的表,这两个表只能按其中相同的一个字段同步,认真研究了一下,用WEB程序进行了处理,可视化显示处理进度,同步操作结果.使用到的 ...

  2. 故事板 — 视图切换(segue)与传值

    1.传值问题:为什么不能给控件的接口赋值 如执行Segue跳转 [self performSegueWithIdentifier:GAPlayeVideo sender:gaVideo]; //在跳转 ...

  3. Nginx 403 error

    nginx 的 403 Forbidden errors 表示你在请求一个资源文件但是nginx不允许你查看.403 Forbidden 只是一个HTTP状态码,像404,200一样不是技术上的错误. ...

  4. 用java 删除mongodb的数据

    import java.net.UnknownHostException; import com.mongodb.BasicDBObject;import com.mongodb.DB;import ...

  5. LeetCode 75

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  6. 转:android git开源项目列表

    1. 知乎的合集说明 https://www.zhihu.com/question/19804692 2. http://p.codekk.com/

  7. backboneJs 导图

  8. Nginx环境下配置PHP使用的SSL认证(https)

    最近一段时间发现好多网站都从http协议变成了加密的https协议,比如说百度.吾志等等.https看起来比http高端了好多,而且在不同的浏览器向上还会显示出不同于http的URL展示效果(比如说c ...

  9. Six important .NET concepts 【Turn】

    Introduction This article will explain six important concepts: stack, heap, value types, reference t ...

  10. Ehcache(2.9.x) - API Developer Guide, Cache Manager Event Listeners

    About CacheManager Event Listeners CacheManager event listeners allow implementers to register callb ...