【PAT甲级 - C++题解】1097 Deduplication on a Linked List

简介: 【PAT甲级 - C++题解】1097 Deduplication on a Linked List

1097 Deduplication on a Linked List


Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.


Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤105) which is the total number of nodes. 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 Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 104, and Next is the position of the next node.


Output Specification:

For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.


Sample Input:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

Sample Output:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1


题意

第一行给定链表起始结点的地址 h 以及结点总数量 n 。


现在要求我们将该链表中数据重复的结点和数据不重复的结点拆分成两个链表并输出,此题对于正数和负数都相同的数据都算重复数据即数据绝对值不能重复。


思路

具体思路如下:


1.先将结点读入到数组当中,方便后续调用。

2.从起始地址开始往后遍历该链表,并用一个数组 st 来判断是否出现重复的数据,如果该数据是第一次出现,则加入数组 a 中,反之加入数组 b 中。

3.分别输出两个链表,注意地址需要是 5 位数字。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, h;
int e[N], ne[N];
bool st[N];
int main()
{
    //输入每个结点的信息
    cin >> h >> n;
    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;
    for (int i = h; i != -1; i = ne[i])
    {
        int data = abs(e[i]);
        if (st[data])    b.push_back(i);
        else
        {
            a.push_back(i);
            st[data] = true;
        }
    }
    //输出不重数据链表
    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]);
    }
    //输出重复数据链表
    for (int i = 0; i < b.size(); i++)
    {
        printf("%05d %d ", b[i], e[b[i]]);
        if (i + 1 == b.size())   puts("-1");
        else printf("%05d\n", b[i + 1]);
    }
    return 0;
}
目录
相关文章
|
算法 C++ 容器
模拟实现c++中的list模版
模拟实现c++中的list模版
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
502 9
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
671 7
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
504 2
|
存储 编译器 C++
C++ initializer_list&&类型推导
在 C++ 中,`initializer_list` 提供了一种方便的方式来初始化容器和传递参数,而右值引用则是实现高效资源管理和移动语义的关键特性。尽管在实际应用中 `initializer_list&&` 并不常见,但理解其类型推导和使用方式有助于深入掌握现代 C++ 的高级特性。
331 4
|
存储 算法 C++
【C++打怪之路Lv10】-- list
【C++打怪之路Lv10】-- list
339 1
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
454 2
|
存储 C++ 容器
C++入门9——list的使用
C++入门9——list的使用
375 1
|
存储 缓存 C++
C++番外篇——list与vector的比较
C++番外篇——list与vector的比较
471 0
|
C++
C++番外篇——list的实现
C++番外篇——list的实现
256 0