PAT甲级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

思路

输入一个链表,输入的同时记录每个结点值的绝对值出现的次数,然后将次数大于1的所有结点放入一个del数组中,表示要去掉的结点。同时原链表删除这些结点。最后输出原链表,再输出del中的结点。

  • 测试点1 考察的是有多余无用的结点,试试这个例子
    87654 3
    99999 -7 87654
    23854 -15 00000
    87654 15 -1
  • 测试点2 考察没有重复的元素,即del元素为0
#include <iostream>
#include <cstdlib>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
	int address;
	int key;
	int next;
};
void output(vector<node> vec);
int main()
{
	int head, n;
	cin >> head >> n;
	map<int, node> mp;
	map<int, int> rec;
	for (int i = 0; i < n; i++)
	{
		node s;
		cin >> s.address >> s.key >> s.next;
		mp[s.address] = s;
	}
	vector<node> list;
	int address = head, index = 0;
	while (address != -1)
	{
		list.push_back(mp[address]);
		rec[abs(list[index++].key)]++; //这一步本来写在上面的for里面,但应该写在这里,这样就只记录表中元素,以防出现不在表中的多余元素
		address = mp[address].next;
	}
	vector<node> del;
	for (int i = list.size() - 1; i >= 0; i--)
	{
		if (rec[abs(list[i].key)] > 1)
		{
			rec[abs(list[i].key)]--;
			del.push_back(list[i]);
			list.erase(list.begin() + i);
			
		}
	}
	reverse(del.begin(), del.end());
	for (int i = 0; i < list.size() - 1; i++)
		list[i].next = list[i + 1].address;
	list[list.size() - 1].next = -1;

	if (del.size() > 0)
	{
		for (int i = 0; i < del.size() - 1; i++)
			del[i].next = del[i + 1].address;
		del[del.size() - 1].next = -1;
	}
	output(list);
	if (del.size() > 0)
		output(del);
	system("pause");
	return 0;
}
void output(vector<node> vec)
{
	if (vec.size() == 1)
		printf("%05d %d %d\n", vec[0].address, vec[0].key, vec[0].next);
	else
	{
		for (int i = 0; i < vec.size() - 1; i++)
			printf("%05d %d %05d\n", vec[i].address, vec[i].key, vec[i].next);
		printf("%05d %d %d\n", vec[vec.size() - 1].address, vec[vec.size() - 1].key, vec[vec.size() - 1].next);
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值