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. 在vs中跑动ransac

    期间遇到很多问题. 记一个最主要的是: LINK2019 无法识别的外部符号,然后某一个函数的函数名 然后是 @@函数名 (@) 大概长成这样.或者还就根本就是 无法识别的外部符号. 解决方案: 我这 ...

  2. Node.js连接Mysql

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

  3. 打印W图案

    一:规律 二维图形的展示都可以使用二维数组来解决,W图形x轴0,1,2,1,0,1,2.....在0到2直接来回的徘徊 y轴是在一直递增........ 二:代码 @Test /** * 测试打印w图 ...

  4. [改善Java代码]对字符串排序 持一种宽容的心态

    在Java中一涉及到中文处理就会冒出很多的问题来,其中的排序也是一个让人头疼的问题,看代码: import java.util.Arrays; public class Client { public ...

  5. [改善Java代码]覆写equals方法时不要识别不出自己

    建议45: 覆写equals方法时不要识别不出自己 我们在写一个JavaBean时,经常会覆写equals方法,其目的是根据业务规则判断两个对象是否相等,比如我们写一个Person类,然后根据姓名判断 ...

  6. C# 实现远程控制软件的关键技术

    一.服务器端多线程Socket技术    用TcpListener进行侦听,接受客户端连接,有客户端连进来后开启处理线程处理数据,代码如下:    using System; using System ...

  7. 7月15日学习之BOM

    setTimeout() //延时器,只执行一次代码 clearTimeout() //清除演示器 setIntervla() //定时器,根据指定时间间隔执行一次代码 clearInterval() ...

  8. hive 未初始化元数据库报错

    启动hive-metastore和hive-server2 用beeline连接hive报错 [root@node04 hive]# beeline Beeline version 0.13.1-cd ...

  9. Agile.Net 组件式开发平台 - 权限管理组件

    RBAC原则       (1)最小权限原则之所以被RBAC所支持,是因为RBAC可以将其角色配置成其完成任务所需要的最小的权限集.       (2)责任分离原则可以通过调用相互独立互斥的角色来共同 ...

  10. 标准库string类型简述

    若想使用标准库的string类需要使用如下声明: #include <string> Using std::string: Using std::wstring: 那么就可以使用这两个类了 ...