1097 Deduplication on a Linked List(链表 结构体与非结构体实现)

博客围绕链表去重问题展开,给定含整数键的单链表,需移除键绝对值重复的节点,并将移除节点存于单独链表。介绍了输入输出规范,分析了遇到的问题,如数组容量设置不当导致测试点未通过,还给出了结构体和非结构体的代码实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/**
链表结点连接点的位置会发生改变,但是结点的初始位置不会发生改变
本题题意:链表去重, 将重复的结点取除掉, 但是重复的结点也要保存下来, 先输出去重后
的链表, 然后输出由去除的重复结点组成的链表
遇到的问题:
    题意中的k值为no more than 10的4次方, 数组容量设为了 10000, 导致两个测试点没过,
    至少设为10001
本题思路:

1.设定结构体存放链表 .()
2.去除无效结点(本题中的无效结点(链表中的重复结点)单独放在c数组中) 将有效结点(
去重后的链表结点放在b数组中)(判断结点数据是否一样是用绝对值是否相等来判断
3.输出 b , c中链表的结点(输出时,b,c数组的第一个结点的起点与终点分别为它们的第一个结点
与第二个结点,至于它们的下一个结点就是它们数组后一个结点的起点)
**/

结构体实现具体代码:

#include<iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
const int maxsize = 1e5 + 5;
struct Node{
	int ad, data, next;
}a[maxsize];
int start, n; 
bool visit[10005];  //判断是否存在重复结点 
vector<Node> b, c; //b存放去重后的链表结点,c存放未去重的链表结点。 
void printLink(vector<Node> l){ //输出链表 
	for(int i = 0; i < l.size(); i++){
		printf("%05d %d ", l[i].ad, l[i].data);
		if(i != l.size() - 1)
			printf("%05d\n", l[i + 1].ad);
		else
			printf("-1\n");
	}
}
int main(){
	scanf("%d%d", &start, &n);
	for(int i = 0; i < n; i++){
		int ad, data, next;
		scanf("%d", &ad);
		scanf("%d%d", &a[ad].data, &a[ad].next);//这里可以判断有没有重复的元素												//只不过这里判断后的元素放入next中是没有顺序的 
		a[ad].ad = ad;
	}
	int temp = start;
	while(start != -1){
		int ab = abs(a[start].data);
		if(visit[ab] == false){
			b.push_back(a[start]);
			visit[ab] = true;
		}else{
			c.push_back(a[start]);
		}
		start = a[start].next;		
	}
	if(temp != -1){
		printLink(b);
		printLink(c);	
	}
	return 0;
} 

非结构体代码实现 (主要是运用了 (#include<unordered_set>)中的find()函数:

#include<iostream>
#include<vector>
#include<unordered_set>
#include<cmath>
using namespace std;
const int maxsize = 1e5 + 5;
int Next[maxsize], data[maxsize];
vector<int> b, c;  //存放的是结点自己的地址 
int n, start;
void printLink(vector<int> l){
	for(int i = 0; i < l.size(); i++){
		printf("%05d %d ", l[i], data[l[i]]);
		if(i != l.size() - 1)
			printf("%05d\n", l[i + 1]); 
		else
			printf("-1\n");
	}
}
int main(){
	scanf("%d%d", &start, &n);
	for(int i = 0; i < n; i++){
		int ad;
		scanf("%d", &ad);
		scanf("%d%d", &data[ad], &Next[ad]);
	}
	unordered_set<int> ab; //这个在  #include<unordered_set>库中 
	int temp = start; 
	while(start != -1){
		if(ab.find(abs(data[start])) == ab.end()){ //表示set中不存在这个元素,就是不重复的结点 
			b.push_back(start); //将此节点的地址加入进b 
			ab.insert(abs(data[start]));
		}else{
			c.push_back(start);
		}
		start = Next[start];
	}
	if(temp != -1){
		printLink(b);
		printLink(c);
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值