【PAT甲级 - C++题解】1133 Splitting A Linked List

简介: 【PAT甲级 - C++题解】1133 Splitting A Linked List

1133 Splitting A Linked List


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 (≤105) which is the total number of nodes, and a positive K (≤103). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.


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 [−105,105], 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


题意

第一行给定链表的起始地址 h、总结点数 n 和常量 k 。


要求将该链表划分成三个部分,将负数放在链表的最前面,正数放到负数的后面,并且数值范围在 [0,k] 的元素放在正数的前面,而大于 k 的元素放在正数的后面。


思路

具体思路如下:


1.先将结点信息存入数组当中,方便后续的调用。

2.准备三个数组 a,b,c ,分别存放负数元素的地址、[0,k] 之间的正数和大于 k 的正数,然后从前往后遍历链表,将元素地址放入对应的数组中。

3.将三个数组合并成一个数组,然后输出最终结果即可。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int h, n, k;
int e[N], ne[N];
int main()
{
    //输入每个结点的信息
    cin >> h >> n >> k;
    for (int i = 0; i < n; i++)
    {
        int address, data, next;
        scanf("%d%d%d", &address, &data, &next);
        e[address] = data, ne[address] = next;
    }
    //对结点进行分类
    vector<int> a, b, c;
    for (int i = h; i != -1; i = ne[i])
    {
        int d = e[i];
        if (d < 0) a.push_back(i);
        else if (d <= k)   b.push_back(i);
        else    c.push_back(i);
    }
    //将划分后的链表合并到一起
    a.insert(a.end(), b.begin(), b.end());
    a.insert(a.end(), c.begin(), c.end());
    //输出最终链表
    for (int i = 0; i < a.size(); i++)
    {
        printf("%05d %d ", a[i], e[a[i]]);
        if (i + 1 == a.size())   puts("-1");
        else    printf("%05d\n", a[i + 1]);
    }
    return 0;
}
目录
相关文章
|
6天前
|
编译器 C++ 容器
【C++/STL】:list容器的深度剖析及模拟实现
【C++/STL】:list容器的深度剖析及模拟实现
11 2
|
5天前
|
编译器 C语言 C++
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
|
6天前
|
存储 C++ 容器
【C++/STL】:list容器的基本使用
【C++/STL】:list容器的基本使用
10 1
|
4天前
|
C++ 容器
【c++】优先级队列|反向迭代器(vector|list)
【c++】优先级队列|反向迭代器(vector|list)
5 0
|
4天前
|
C++
【c++】list模拟实现(2)
【c++】list模拟实现(2)
9 0
|
4天前
|
C++
【c++】list模拟实现(1)
【c++】list模拟实现(1)
12 0
|
4天前
|
存储 C++ 容器
C++之list容器
C++之list容器
6 0
|
5天前
|
容器
C++11 列表初始化(initializer_list),pair
C++11 列表初始化(initializer_list),pair
|
5天前
|
编译器 C++ 容器
【C++】学习笔记——list
【C++】学习笔记——list
7 0
|
2天前
|
C++
【C++】日期类Date(详解)②
- `-=`通过复用`+=`实现,`Date operator-(int day)`则通过创建副本并调用`-=`。 - 前置`++`和后置`++`同样使用重载,类似地,前置`--`和后置`--`也复用了`+=`和`-=1`。 - 比较运算符重载如`&gt;`, `==`, `&lt;`, `&lt;=`, `!=`,通常只需实现两个,其他可通过复合逻辑得出。 - `Date`减`Date`返回天数,通过迭代较小日期直到与较大日期相等,记录步数和符号。 ``` 这是236个字符的摘要,符合240字符以内的要求,涵盖了日期类中运算符重载的主要实现。