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. Java泛型读书笔记 (三)

    泛型对于老代码的支持 Java的泛型设计成类型擦除的目的,很大一部分是为了兼容老老代码.如下的一段代码: void setLabelTable(Dictionary table) table的类型是非 ...

  2. Python lib库docker-py和docker的区别

    1)两者的安装方式 pip install docker A Python library for the Docker Engine API pip install docker-py A Pyth ...

  3. 转载 【Linux】Linux中常用操作命令

    [Linux]Linux中常用操作命令     https://www.cnblogs.com/laov/p/3541414.html#vim   Linux简介及Ubuntu安装 常见指令 系统管理 ...

  4. 树链剖分【洛谷P3833】 [SHOI2012]魔法树

    P3833 [SHOI2012]魔法树 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节 ...

  5. 序列终结者 BZOJ 1251 Splay

    题目背景 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

  6. clojure with postgres

    主要关注访问pg.不关心其他db 1 clojure.java.jdbc https://github.com/clojure/java.jdbc http://clojure-doc.org/art ...

  7. aspectj 与 spring 自定义注解

    ** * ErrorCode: * * @author yangzhenlong * @since 2016/7/21 */ @Target({ElementType.METHOD}) @Retent ...

  8. Kibana问题搜集---下载源码,执行npm install 报错

    npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! chromedriver@2.34.0 install: `node install.js`npm E ...

  9. JAVA Font类

    java.awt.Font 设计字体显示效果 Font mf = new Font(String 字体,int 风格,int 字号); 字体:TimesRoman, Courier, Arial等 风 ...

  10. SSH防入侵

    1.1 ssh防止入侵方法说明 如何防止SSH登录入侵小结: 1.用密钥登录,不用密码登陆 2.牤牛阵法:解决SSH安全问题 a.防火墙封闭SSH,指定源IP限制(局域网.信任公网) b.开启SSH只 ...