Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤) which is the total number of nodes, and a positive K (≤). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer in [, and Next is the position of the next node. It is guaranteed that the list is not empty.

Output Specification:

For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218

Sample Output:

33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1
【题解】
新建3条链表,分别存负数,[0,k]的数以及大于k的数。然后将三条链表窜起来
当然,更省事的,就直接使用3个queue分别记录三组数据,然后输出,
两个方法差不多,容器的更方便,但链表的不用额外空间
 #include <iostream>
#include <vector>
using namespace std;
struct Node
{
int val, next;
}List[];
int head, n, k;
int main()
{
cin >> head >> n >> k;
while (n--)
{
int address, data, next;
cin >> address >> data >> next;
List[address].val = data;
List[address].next = next;
}
int head1 = , head2 = , head3 = ;//分别是负数、中间数、>k数的链表
int p = head, p1 = head1, p2 = head2, p3 = head3;
while (p != -)
{
if (List[p].val < )
{
List[p1].next = p;
p1 = p;
}
else if (List[p].val > k)
{
List[p3].next = p;
p3 = p;
}
else
{
List[p2].next = p;
p2 = p;
}
p = List[p].next;
}
//这里的顺序千万不要反了,因为next不是地址,要先改变,再赋值
List[p3].next = -;
List[p2].next = List[head3].next;
List[p1].next = List[head2].next;
p = List[head1].next;
while (List[p].next != -)
{
printf("%05d %d %05d\n", p, List[p].val, List[p].next);
p = List[p].next;
}
printf("%05d %d %d\n", p, List[p].val, List[p].next);
return ;
}
												

PAT甲级——A1133 Splitting A Linked List【25】的更多相关文章

  1. PAT A1133 Splitting A Linked List (25) [链表]

    题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...

  2. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  3. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  4. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  5. PAT A1133 Splitting A Linked List (25 分)——链表

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  6. A1133. Splitting A Linked List

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  7. PAT甲级 1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  8. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  9. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

随机推荐

  1. 原生js如何获取某一元素的高度

    三种方法: 1.document.getElementById("id").style.height,这种方法的前提是必须之前已经显示的在css中声明过height,才能取得正确的 ...

  2. 自定义alert 确定、取消功能

    以删除为例,首先新建html <table border="1" cellpadding="0" cellspacing="0" id ...

  3. mybatis浅显认识

    mybatis主配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configu ...

  4. C#标准的事件模型

    事件模型的几个规范: 委托类型的名称以EventHandler结束: 委托原型返回值为void; 委托原型具有两个参数:sender表示事件触发者,e表示事件参数: 事件参数的名称以EventArgs ...

  5. this.$router.go()和this.$router.push()的差别

    1.this.$router.go(val) => 在history记录中前进或者后退val步,当val为0时刷新当前页面. 2.this.$router.push(path) => 在h ...

  6. linux 平台安装JDK环境

    1.检查一下系统中的jdk版本 [root@localhost software]# java -version 2.检测jdk安装包 [root@localhost software]# rpm - ...

  7. codeforces round#524 D - Olya and magical square /// 大概算是数学规律题?

    题目大意: t 个测试用例  (1≤t≤103) 给定n k  (1≤n≤10^9,1≤k≤10^18) 表示有一个边长为2^n的正方形格子 每次操作只能将一个格子切割为左上左下右上右下的四等分格子 ...

  8. vue-router 的重定向-redirect

    1. 我们主要在index.js重新设置路由的重新定向redirect参数 index.js import Vue from 'vue' import Router from 'vue-router' ...

  9. 学习mysql水平分区和实践笔记

    SHOW PLUGINS; sql 可以查看partition的Status 是否是ACTIVE的 使用mydatetime 进行水平分区案例: CREATE TABLE test_users ( ` ...

  10. 蛮好用的Gungho重点工作督查督办跟踪管理系统

    重点工作督查督办跟踪管理系统可以实现: 为了确保上级重要决定.指示和本单位重大目标和工作部署及时落到实处,确定实效,提升办事效率. 重点工作督查督办事项包括: 1)上级单位或领导的批示指示: 2)公司 ...