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 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
/**
链表结点连接点的位置会发生改变,但是结点的初始位置不会发生改变
本题题意:链表去重, 将重复的结点取除掉, 但是重复的结点也要保存下来, 先输出去重后
的链表, 然后输出由去除的重复结点组成的链表
遇到的问题:
题意中的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;
}