Linked List Sorting (链表)

时间:2023-01-31 23:30:37

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 &lpar;25 分&rpar;(数组模拟链表,没注意到不一定所有节点都在链表里)

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

  2. PAT 解题报告 1052&period; Linked List Sorting &lpar;25&rpar;

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

  3. PAT1052&colon;Linked List Sorting

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

  4. PAT 1052 Linked List Sorting &lbrack;一般&rsqb;

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

  5. Pat 1052 Linked List Sorting &lpar;25&rpar;

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

  6. pat1052&period; Linked List Sorting &lpar;25&rpar;

    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. &lbrack;CareerCup&rsqb; 2&period;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 &lpar;25&rpar;(25 分)

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

随机推荐

  1. yii2-basic后台管理功能开发之四:图片上传FileInput

    我采用的是 kartik-v/yii2-widget-fileinput的文件上传插件,大家可以去github查看详细的安装方法和使用说明. 需求:上传图片+可以预览缩略图 在这里说说我碰到的问题:限 ...

  2. Spark RDD API详解&lpar;一&rpar; Map和Reduce

    RDD是什么? RDD是Spark中的抽象数据结构类型,任何数据在Spark中都被表示为RDD.从编程的角度来看,RDD可以简单看成是一个数组.和普通数组的区别是,RDD中的数据是分区存储的,这样不同 ...

  3. MySQL SELECT语句

    说明:MySQL的offset第一行是0 位置指的是在SELECT语句中第几个出现的字段,如:1,则代表用第一个出现的字段来分组. SELECT语句: SELECT  select_expr1 [,s ...

  4. Qt widgets

    1,Qt部件Widgets--CheckWidgets,安置其他部件的Widgets,让用户选择数值的部件 选择部件---使用户能够从预定义的条目菜单中做出选择,combination QListBo ...

  5. WebSphere--安装与配置

    对于任何软件,都需要一些计划和具体步骤以确保成功安装.对于安装与配制 WebSphere应用服务器及其组件也是如此.下面介绍在Windows NT 上安装与配置WebSphere应用服务器     1 ...

  6. python基础之小数据池、代码块、编码和字节之间换算

    一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...

  7. CCNA学前基础一

    网络设备: 集线器:集线器就是一种采用共享式工作状态的设备.Hub将信号放大后传输给其他端口,即传输线路是共享的. 交换机:用于连接终端设备,和基本的安全功能还有广播域的隔离.优点实现多用户同时访问, ...

  8. postfix 指定用户限制指定域名收发

    main.cf 配置示例: smtpd_restriction_classes = local_in_only, local_out_only local_in_only = check_recipi ...

  9. Java 多线程(七) 线程间的通信——wait及notify方法

    线程间的相互作用 线程间的相互作用:线程之间需要一些协调通信,来共同完成一件任务. Object类中相关的方法有两个notify方法和三个wait方法: http://docs.oracle.com/ ...

  10. js document&period;activeElement 获得焦点的元素

    <body> <input type="text" id="test" value="ajanuw"> <sc ...