1097 Deduplication on a Linked List (25分)

1 题目

1097 Deduplication on a Linked List (25分)
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 (≤10^5​​ ) 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 10^​4 , 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

2 分析

  • 题意:把链表中data绝对值重复的元素删除(只保留第一个),把未删除的结点按顺序输出,把删除的结点按原链表顺序输出
  • 思路:
    • 1 创建链表
      • 数据、地址、下一个结点地址、排序编号
    • 2 初始化
      • 令order都为2*MAXN
    • 3 枚举链表
      • CountVaild(从0开始,记录有效结点),CountVail(从0开始,记录被删除结点)
      • 如果链表的结点值的绝对值没出现过(使用一个全局的bool isExist来记录,初始化未false),则令isExist的值未true,修改结点的order为CountVaild++
      • 否则,修改结点的order为MAXN + CountVaild++
    • 4 排序(根据order的数值)
    • 5 输出结果

3 参考代码

#include <cstdio>
#include <stdlib.h>
#include <algorithm>

using std::sort;

const int MAXN = 100010;
bool Appear[MAXN] = {false};//绝对值映射是否出现过

struct Node{
    int m_data,m_address,m_next;
    int order;
}node[MAXN];//1 定义静态链表

bool cmp(Node a, Node b){
    return a.order < b.order;
}//按order从小大排序

int main(){
    for (int i = 0; i != MAXN ; ++i) {//2 初始化
        node[i].order = MAXN * 2;//所有结点都是无效结点
    }

    int n, head;
    scanf("%d%d", &head, &n);

    int addres;
    while (n--){//输入所有结点
        scanf("%d", &addres);
        scanf("%d%d", &node[addres].m_data, &node[addres].m_next);
        node[addres].m_address = addres;
    }

    int countDel = 0, countVail = 0;//分别表示删除结点和有效结点的数量
    for (int j = head; j != -1 ; j = node[j].m_next) {//3 枚举链表
        if(Appear[abs(node[j].m_data)] == false){//绝对值不存在
            Appear[abs(node[j].m_data)] = true;//标记为已存在
            node[j].order = countVail++;//不删除,编号从0开始
        }else{
            node[j].order = MAXN + countDel++;//被删除,编号从maxn开始
        }
    }

    int count = countDel + countVail;//有效结点个数
    sort(node, node + MAXN, cmp);//4 按order从小到大排序

    for (int k = 0; k != count ; ++k) {//5 输出结果
        if(k != countVail - 1 && k != count - 1){
            printf("%05d %d %05d\n", node[k].m_address, node[k].m_data, node[k+1].m_address);
        }
        else{//最后一个结点单独处理
            printf("%05d %d -1\n", node[k].m_address, node[k].m_data);
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星蓝雨

如果觉得文章不错,可以请喝咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值