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 (≤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

-------------------------------------这是题目和解题的分割线-------------------------------------

参考的代码,自己写的虽然有一半的思路是一样的,但另一半歪不知哪去了......

其实这道题并不需要完全模拟链表,不然代码又冗杂又容易出错。

只需要分开处理出现和未出现的情况,并记录其顺序,然后排序,最后输出next地址时以i+1.address替代就可以免去模拟链表的麻烦了。

#include<cstdio>
#include<algorithm>

using namespace std;

const int maxN = 100010;

struct node
{
	//需要存取当前地址,最后输出并不是靠数组下标 
	int address; 
	int next;
	int data;
	//记录顺序,用在数据的绝对值是否相等处,以此将两个链表分开 
	int order;
}list[maxN];

//按照顺序排序 
//node不是list,用结构体的名字而不是结构体定义的变量名 
bool cmp(node a,node b)
{
	return a.order<b.order;
}

//表示该数字的绝对值是否出现过 
int flag[maxN] = {0};

int main()
{
	int m,n,i;
	scanf("%d%d",&m,&n);
	int address;
	//必须0-maxN-1都置成最大的数,以便下面排序 
	for(i=0;i<maxN;i++)
		list[i].order = 2*maxN;	
	for(i=0;i<n;i++)
	{
		scanf("%d",&address);
		scanf("%d%d",&list[address].data,&list[address].next);
		list[address].address = address;
	}
	int order1 = 0,order2 = 0,start = m;
	//遍历 
	while(start!=-1)
	{
		int x = abs(list[start].data);
		//如果出现过,顺序等于order2++,并加上maxN来加以区分
		if(flag[x]==1)
			list[start].order = maxN + order2++;
		//如果未出现,order1++,并修改状态 
		else 
		{
			list[start].order = order1++;
			flag[x] = 1;
		}	
		start = list[start].next;
	}
	//全组排序,原先的数组下标会改变,所以结构体需记录当前地址 
	sort(list,list+maxN,cmp);
	for(i=0;i<order1+order2;i++)
	{
		//不是最后一个结点 
		if(i!=order1-1&&i!=order1+order2-1)
			printf("%05d %d %05d\n",list[i].address,list[i].data,list[i+1].address);
		else
			printf("%05d %d -1\n",list[i].address,list[i].data);	
	}
}

时隔好久,自己写了个,感觉比上面的要好理解一些。

#include<cstdio>
#include<cstdlib>

struct node
{
	int address;
	int data;
	int next;
}list[100010];

int main()
{
	int s1,n,i,test[100010] = {};
	int a[100010],b[100010];
	scanf("%d%d",&s1,&n);
	for(i=0;i<n;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		list[a].address = a;
		list[a].data = b;
		list[a].next = c;
	}
	int tmp = s1,cnt1 = 0,cnt2 = 0;
	while(s1!=-1)
	{
		int x = abs(list[s1].data);	
		if(test[x]==1)
			b[cnt2++] = list[s1].address;
		else a[cnt1++] = list[s1].address;
		s1 = list[s1].next;
		test[x] = 1;
	}
	for(i=0;i<cnt1;i++)
	{
		printf("%05d %d",a[i],list[a[i]].data);
		if(i==cnt1-1) printf(" -1\n");
		else printf(" %05d\n",list[a[i+1]].address);
	}
		
	for(i=0;i<cnt2;i++)
	{
		printf("%05d %d",b[i],list[b[i]].data);
		if(i==cnt2-1) printf(" -1\n");
		else printf(" %05d\n",list[b[i+1]].address);
	}
	return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值