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. 防止vuejs在解析时出现闪烁

    ---## 防止vuejs在解析时出现闪烁 ## 原因: 在使用vuejs.angularjs开发时,经常会遇见在如Chrome这类能够快速解析的浏览器上出现表达式({{ express }} ),或 ...

  2. Node.js连接Mysql

    1.安装 npm install mysql 注意要复制node_modules到当前工程的文件夹中 2.基本连接 /** * Created by Administrator on 13-12-25 ...

  3. Java 文件名操作的相关工具类

    项目中的操作文件名的工具类: import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; ...

  4. hdu 4374 单调队列优化动态规划

    思路:我只想说,while(head<=rear&&que[rear].val+sum[j]-sum[que[rear].pos-1]<=dp[i-1][j]+num[i- ...

  5. Jersey(1.19.1) - Extracting Request Parameters

    Parameters of a resource method may be annotated with parameter-based annotations to extract informa ...

  6. Spring(3.2.3) - Beans(6): 作用域

    Spring 支持五种作用域,分别是 singleton.prototype.request.session 和 global session. 作用域 说明  singleton (默认作用域)单例 ...

  7. Linux 命令 - watch: 反复执行命令,全屏显示输出

    watch 命令周期性地执行命令,全屏显示输出.可以通过 watch 命令反复执行某一程序来监视它的输出变化. 命令格式 watch [-dhvt] [-n <seconds>] [--d ...

  8. 第三十三篇、富文本 NSMutableAttributedString

    // 设置颜色等 NSMutableDictionary *arrDic = [NSMutableDictionary dictionary]; arrDic[NSForegroundColorAtt ...

  9. Cocos移植到Android-通过命令行交叉编译打包

    当我们在Windows下使用Visual Studio开发好Win32平台的游戏程序后,我们需要把它们移植到不同的平台上.Cocos2d-x支持很多个平台,然而,我们不可能介绍全部平台的移植.我们总结 ...

  10. vJine 第三波 之 Lua 来袭 vJine.Lua

    vJine.Lua vJine.Lua是Lua语言的C#封装库,可实现通过C#直接运行Lua脚本并与Lua脚本交互的功能. 1. 授权: MPL2.0 相关资源: nuget:(https://www ...