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 (≤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 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
分析:
给定一段带有关键字的单链表,要求遍历单链表,保存关键字的绝对值第一次出现的结点,移去不符合要求的结点。
保存关键字的绝对值第一次出现的结点,这是什么意思呢?
就用例题来解释,根据输入可形成如下单链表:
00100 23854 00000 99999 87654
21 -15 -15 -7 15
23854 00000 99999 87654 -1
第一次遇到21,保留地址为00100的结点;
接着第一次遇到15,保留地址为23854的结点;
又碰到15,移去地址为00000的结点;接着继续遍历链表…

解决方法:
遍历链表时,直接修改结点的next的地址即可。
参考代码:(代码虽然看着多,其实理解了很简单)

#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
struct node {
    int address;
    int data;
    int next;
};
using namespace std;
int main()
{

    int s, n;
    scanf_s("%d%d", &s, &n);
    int address, data, next;
    int origin_st = s, origin_end = s;
    int remove_st = -1, remove_end;
    int temp = s;
    map<int, node>link;
    set<int>sets;


    for (int i = 1; i <= n; i++) {
        scanf_s("%d%d%d", &address, &data, &next);

        link[address].address = address;
        link[address].data = data;
        link[address].next = next;
    }

    while (temp != -1) {
        int num = abs(link[temp].data);

        if (!sets.count(num)) {    //不存在
            sets.insert(num);

            if (temp!= s) {
                link[origin_end].next = temp;
                origin_end = temp;
            }

        }
        else {                     //存在
            sets.insert(num);
            if (remove_st == -1) {
                remove_st = temp;
                remove_end = temp;
            }

            else {
                link[remove_end].next = temp;
                remove_end = temp;
            }

        }
        temp = link[temp].next;
    }

    link[origin_end].next = -1;
    link[remove_end].next = -1;
    int ts = origin_st;

    while (ts != -1)
    {
        printf("%05d %d ", link[ts].address, link[ts].data);
        if (link[ts].next != -1) {
            printf("%05d", link[ts].next);
        }
        else printf("-1");
        printf("\n");
        ts = link[ts].next;
    }
    ts = remove_st;
    while (ts != -1)
    {
        printf("%05d %d ", link[ts].address, link[ts].data);
        if (link[ts].next != -1) {
            printf("%05d", link[ts].next);
        }
        else printf("-1");
        printf("\n");
        ts = link[ts].next;
    }

    return 0;
}

欢迎大家评论!!!

猜你喜欢

转载自blog.csdn.net/ssf_cxdm/article/details/82596464