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

题意:
        给出链表第一个结点的地址,有5个结点,然后下列给出5个结点的地址,数据域和指针域,如果后面有出现数据域的绝对值和前面结点的数据域的绝对值相等的结点,就要删除这个结点,如题意得原链表的结点顺序为

地址:0010->23854->00000->999999->87654->-1

数据:   21      -15        -15          -7          15

由此可见000000和87654的数据域的绝对值和23854的相等,所以要删除这两个结点

就变成了0010->23854->999999->-1

然后我们要输出删除后的链表信息,输出完毕后最后还要按删除的顺序输出删除的结点的信息(删除的结点要按顺序重新组成链表);

思路:

        遍历链表,给结点编号,如果后面的结点的数据域已经出现过了就不给其编号,将其放到另外一个数组内,然后原链表按order排序,有效结点都在数组的最左端,该链表的数据域的绝对值没有相同的,另外一个存放删除结点的数组建立新的链表,分别把这两个链表输出就好了;

代码:

代码:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct Node{
	int data;
	int next=-1;
	int order=maxn;
	int address;
}node[maxn],deletenode[maxn];
bool isexit[maxn]={false};
bool cmp(Node a,Node b){
	return a.order<b.order;
}
int main(){
	int head,n,id,data,next;
	cin>>head>>n;
	for(int i=0;i<n;i++){
		cin>>id>>data>>next;
		node[id].address=id;
		node[id].data=data;
		node[id].next=next;
	}
	int p=head;
	int cnt=0,deletenum=0;
	while(p!=-1){
		if(isexit[abs(node[p].data)]==false){//说明没重复; 
			node[p].order=cnt++;
			isexit[abs(node[p].data)]=true;
		}else {
			deletenode[deletenum++]=node[p];
		}
		p=node[p].next; 
	}
	sort(node,node+maxn,cmp);//order为maxn的结点会被排序得到处都是不是连续的跟在链表后面的; 
	for(int i=0;i<cnt;i++){
		printf("%05d %d ",node[i].address,node[i].data);
		i==cnt-1?printf("-1\n"):printf("%05d\n",node[i+1].address);
	}
	for(int i=0;i<deletenum;i++){
		printf("%05d %d ",deletenode[i].address,deletenode[i].data);
		i==deletenum-1?printf("-1\n"):printf("%05d\n",deletenode[i+1].address);
	}	
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值