PAT 甲级A1097 Deduplication on a Linked List (25 分)

题目:

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

题意:

  将链表中绝对值相同的结点删去,且删去的结点保留在新的链表中,需要输出。原链表保留第一个绝对值相等的数。

思路:

  1、利用静态链表,且置data域的值为inf(比题中最大值更大即可),用以判断是否为有效链表的结点。且开设三个链表,一个为node[maxn],用其下标存储初始读入的结点地址,方便处理,故由此可知,maxn应比地址数位大一位,6位以上。ans[maxn]用以存储保留下来的链表。removed[maxn],用以存储被删去的结点。静态链表的结构体的结构参考下面代码。

  2、开辟一个hashtable,用以判断读入的结点其绝对值是否为有没有重复,按链表顺序读入的第一个hashtable==false的点一定是绝对值相同的数中的第一个数。

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int inf=0x7FFFFFFF;
struct Node{
	int address,data,next;
	Node(){
		data=inf;
	}
}node[maxn],ans[maxn],removed[maxn];
bool hashtable[maxn]={0};

int main(){
	int b,n;
	scanf("%d%d",&b,&n);
	for(int i=0;i<n;++i){
		int address,key,next;
		scanf("%d%d%d",&address,&key,&next);
		node[address].data=key,node[address].next=next;
	}
	int now=b,cnt1=0,cnt2=0;
	while(now!=-1&& node[now].data!=inf ){
		int temp=node[now].data,next=node[now].next;
		if(!hashtable[abs(temp)]){
			ans[cnt1].address=now, ans[cnt1].data=temp, ans[cnt1].next=next;
			cnt1++;
			hashtable[abs(temp)]=1;
		}else{
			removed[cnt2].address=now, removed[cnt2].data=temp, removed[cnt2].next=next;
			cnt2++;
		}
		now=node[now].next;
	}
	for(int i=0;i<cnt1;++i){
		printf("%05d %d ",ans[i].address,ans[i].data);
		if(i!=cnt1-1)	printf("%05d\n",ans[i+1].address);
		else	printf("-1\n");
	}
	for(int i=0;i<cnt2;++i){
		printf("%05d %d ",removed[i].address,removed[i].data);
		if(i!=cnt2-1)	printf("%05d\n",removed[i+1].address);
		else	printf("-1\n");
	}
	return 0;
}

如有新想法即会及时更新。 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值