A1097 Deduplication on a Linked List [链表去重]

在这里插入图片描述

#include<vector>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<map>
#include<cstring>
#include<string>
#include<queue>
#include<array>
#include<stack>
using namespace std;
const int maxn = 100010;
const int TABLE = 10001;
struct Node
{
	int address,data,next;
	bool flag;
	int order;
}node[maxn];
bool cmp(Node a, Node b)
{
	return a.order < b.order;
}
bool isExist[TABLE];
int main()
{
	memset(isExist, false, sizeof(isExist));
	for (int i = 0; i < maxn; i++)
	{
		node[i].order = 2 * maxn;
	}
	int n, begin, address;
	cin >> begin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> address;
		cin >> node[address].data >> node[address].next;
		node[address].address = address;
	}
	int countvalid = 0, countRemoved = 0, p = begin;
	while (p != -1)
	{
		if (!isExist[abs(node[p].data)]) {
			isExist[abs(node[p].data)] = true;
			node[p].order = countvalid++;
		}
		else
		{
			node[p].order = maxn + countRemoved++;
		}
		p = node[p].next;
	}
	sort(node, node + maxn, cmp);
	int count = countvalid + countRemoved;
	for (int i = 0; i < count; i++)
	{
		if (i != countvalid - 1 && i != count - 1)
		{
			printf("%05d %d %05d
", node[i].address, node[i].data,node[i+1].address);
		}
		else
		{
			printf("%05d %d -1
", node[i].address, node[i].data);
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Hsiung123/p/13812025.html