1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

链表的插入和遍历。注意链表可以为空。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<stack>
using namespace std;
struct node{
int k,next;
};
#define h 100000
node line[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int first,n;
scanf("%d %d",&n,&first);
if(first==-){//链表可能为空,这个太坑!!
printf("0 -1\n");
return ;
}
int i,add,k,next;
for(i=;i<n;i++){
scanf("%d %d %d",&add,&k,&next);
line[add].k=k;
line[add].next=next;
}
line[h].next=-;
int p,pp,cur,count=;
next=line[first].next;
line[first].next=line[h].next;
line[h].next=first;
while(next!=-){//insert
cur=next;
pp=h;//头节点,指向p的前一个节点
p=line[h].next; //cout<<"p: "<<p<<" "<<line[p].k<<endl;
//cout<<"cur: "<<cur<<" "<<line[cur].k<<endl; while(p!=-&&line[p].k<line[cur].k){
pp=p;
p=line[p].next; //cout<<pp<<" "<<p<<endl; } //cout<<"cur"<<endl; next=line[cur].next;
line[cur].next=p;
line[pp].next=cur;
count++;
}
next=line[h].next;
printf("%d %05d\n",count,next);
while(next!=-){//遍历
printf("%05d %d ",next,line[next].k);
if(line[next].next==-){
printf("%d",line[next].next);
}
else{
printf("%05d",line[next].next);
}
printf("\n");
next=line[next].next;
}
return ;
}

pat1052. Linked List Sorting (25)的更多相关文章

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

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

  2. PAT1052:Linked List Sorting

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

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

  4. Pat 1052 Linked List Sorting (25)

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

  5. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

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

  6. 1052. Linked List Sorting (25)

    题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...

  7. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

  8. PAT Advanced 1052 Linked List Sorting (25) [链表]

    题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...

  9. PAT (Advanced Level) 1052. Linked List Sorting (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

随机推荐

  1. 海量推荐系统:mapreduce的方法

    1. Motivation 2. MapReduce MapReduce是一种数据密集型并行计算框架. 待处理数据以"块"为单位存储在集群机器文件系统中(HDFS),并以(key, ...

  2. [raspberry pi3] 安装ffmpeg

    买了个pi3,pi相对于通常的嵌入式系统的最大好处是里面夹带了gcc编译器,有很多东西都不需要交叉编译了. arm和pc的性能还是不能比的,io的瓶颈还是很明显的, 想要编的快点还是要在pc上交叉编译 ...

  3. nfs搭建和挂载

    1.搭建server a.创建共享目录 mkdir /nfs1 b.vim /etc/sysconfig/nfs     固定端口 c.vim /etc/export /nfs1 192.168.10 ...

  4. linux 进程间通信机制(IPC机制)- 管道

    一,定义: 管道又可以分为无名管道和命名管道,两者的用途是不一样的. 无名管道PIPE:主要用于具有亲缘关系的进程之间的通信,无名管道的通信是单向的,只能由一段到另外一段:无名管道是临时性的,完成通信 ...

  5. c++类 初始化const数据成员

    对于const或引用类型的类成员,唯一的初始化方式是在构造函数中使用成员函数初始化列表.构造函数体中的赋值语句是无效的 正确 #include<iostream> using namesp ...

  6. 重装iTunes 错误代码42401 解决办法

    昨晚手贱点击从iTunes 11升级到iTunes12,之后发现iTunes 12各种卡,简直不能忍,然后直接拉iTunes 12到AppClear,然后安装iTunes 11,安装完成之后打开iTu ...

  7. mybatis逆向工程工具

    mybatis逆向工程 package com.cxy; import java.io.File; import java.util.*; import org.mybatis.generator.a ...

  8. SwiftMailer 发送邮件时 提示fsockopen() 被禁用

    站点转移空间,发送邮件的SwiftMailer 类提示错误如下: Warning: fsockopen() has been disabled for security reasons in D:\1 ...

  9. [JavaScript]instanceof String not behaving as expected in Google Apps Script

    Link: http://stackoverflow.com/questions/11571923/instanceof-string-not-behaving-as-expected-in-goog ...

  10. 在Ubuntu中使用AppImage类型文件

    右键--属性---权限---允许作为执行文件启动