【PAT甲级 - C++题解】1052 Linked List Sorting

简介: 【PAT甲级 - C++题解】1052 Linked List Sorting

1052 Linked List Sorting


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

题意

第一行首先包含一个整数 N NN,表示总节点数量,然后包含链表头节点的地址。


接下来 N NN 行,每行描述一个节点的信息,格式如下:

Address Key Next

其中 Address 是节点地址,Key 值是一个整数,Next 是下一个节点的地址。


地址都是 5 位正整数,可能有前导 0 。


保证各节点的 Key 值互不相同,且链表中不存在环形结构。


第一行首先输出一个整数表示链表的总节点数量,然后输出排序后的链表头节点地址。


接下来若干行,从头节点开始,依次输出每个节点的信息,格式与输入相同。


注意:


本题中可能包含不在链表中的节点,这些节点无需统计,无需排序,无需输出。

链表可能为空。(头节点地址为 −1)。

思路


  1. 先将所有结点用一个哈希表存起来,key 是结点的地址,value 是结点的三个信息,用结构体存储。
  2. 根据给定的头结点,将链表中的结点放到另一个容器当中。
  3. 对容器中的结点进行排序,然后输出。注意,最后一个结点的 next 要输出 -1

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
struct Node {
    string address;
    int key;
    string next;
    //设定排序规则
    bool operator <(const Node& x)const
    {
        return key < x.key;
    }
};
int main()
{
    int n;
    string head;
    cin >> n >> head;
    //输入结点信息
    unordered_map<string, Node> num;
    for (int i = 0; i < n; i++)
    {
        char address[10], next[10];
        int key;
        scanf("%s %d %s", address, &key, next);
        num[address] = { address,key,next };
    }
    //将存在链表中的筛选出来
    vector<Node> res;
    for (string i = head; i != "-1"; i = num[i].next)    res.push_back(num[i]);
    cout << res.size();
    if (res.empty()) puts(" -1");
    else
    {
        sort(res.begin(), res.end()); //对链表中结点的key值从小到大进行排序
        cout << " " << res[0].address << endl;
        for (int i = 0; i < res.size(); i++)
        {
            if (i + 1 == res.size())  //如果没有遍历到最后一个结点
                printf("%s %d -1\n", res[i].address.c_str(), res[i].key);
            else  //如果遍历到最后一个结点
                printf("%s %d %s\n", res[i].address.c_str(), res[i].key, res[i + 1].address.c_str());
        }
    }
    return 0;
}


目录
相关文章
|
16天前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
20 1
|
29天前
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
47 7
|
1月前
|
存储 编译器 C++
C++ initializer_list&&类型推导
在 C++ 中,`initializer_list` 提供了一种方便的方式来初始化容器和传递参数,而右值引用则是实现高效资源管理和移动语义的关键特性。尽管在实际应用中 `initializer_list&&` 并不常见,但理解其类型推导和使用方式有助于深入掌握现代 C++ 的高级特性。
22 4
|
3月前
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
73 2
|
3月前
|
存储 算法 C++
【C++打怪之路Lv10】-- list
【C++打怪之路Lv10】-- list
26 1
|
3月前
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
78 5
|
3月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
91 2
|
3月前
|
存储 缓存 C++
C++番外篇——list与vector的比较
C++番外篇——list与vector的比较
32 0
|
3月前
|
C++
C++番外篇——list的实现
C++番外篇——list的实现
28 0
|
3月前
|
存储 C++ 容器
C++入门9——list的使用
C++入门9——list的使用
25 0