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. 使用sql对比Mysql中数据库2个表结构

    比较两个数据表的结构 SELECT column_name, max( CASE WHEN table_name = 'table1' AND table_schema = 'db1' THEN 'Y ...

  2. 小程序学习三 一切的开始app() 小程序的注册

    现在打开 app.js //app.js App({ onLaunch(options) { //小程序初始化 // console.log("小程序初始化", options) ...

  3. ArrayList集合二

    集合的遍历 通过集合遍历,得到集合中每个元素,这是集合中最常见的操作.集合的遍历与数组的遍历很像,都是通过索引的方式,集合遍历方式如下 13 import java.util.ArrayList; 1 ...

  4. 使用JS实现快速排序

    大致分三步: 1.找基准(一般是以中间项为基准) 2.遍历数组,小于基准的放在left,大于基准的放在right 3.递归 function quickSort(arr){ //如果数组<=1, ...

  5. 下面是一段delphi代码,你在c# 中引入api 即可

    procedure TForm1.Button1Click(Sender: TObject);var i:HWND; cs:CREATESTRUCT;begin// i := FindWindowEx ...

  6. hdu多校第一场1004(hdu6581)Vacation 签到

    题意:有n+1辆车,每辆车都有一定的长度,速度和距离终点的距离,第1-n辆车在前面依次排列,第0辆车在最后面.不允许超车,一旦后车追上前车,后车就减速,求第0辆车最快什么时候能到达终点? 思路:对于每 ...

  7. ionic:目录

    ylbtech-ionic:目录 1.返回顶部 1. http://www.runoob.com/ionic/ionic-tutorial.html 2. 2.返回顶部   3.返回顶部   4.返回 ...

  8. 在Debian中安装VNC Server

    大部分情况下我们用ssh就可以登录linux服务器了.但有时候我们的程序需要在图形界面下运行,这时我们就要用到vnc server这个软件了. 在Debian下安装vnc server很简单的,只要几 ...

  9. Red and Black 模板题 /// BFS oj22063

    题目大意: Description There is a rectangular room, covered with square tiles. Each tile is colored eithe ...

  10. Linux CPU负载状态:%us/%sy/%ni/%id/%wa/%hi/%si/%st含义

    原文 Linux CPU负载状态:%us/%sy/%ni/%id/%wa/%hi/%si/%st含义 缙哥哥发现用了雅黑的探针,在 Linux 的 CPU 状态信息中发现,有“%us.%sy.%ni. ...