PAT 甲级 1052 Linked List Sorting

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (<105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [−105,105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

题意:有5个结点,输入第一个结点的地址,下面5行分别是结点的地址,数据域,next域,给这五个结点按数据域的大小从小到大排列,重新建立链表,输出有多少个有效的结点,和第一个结点的地址,然后输出各个结点的地址,数据域和next域

思路:因为地址是五位数,可以用静态链表,第一步是建立链表,然后第二步是给链表排序,排序后重新连接结点,最后一步是输出链表信息

总结:

一,结点的排序一般要使用二级排序(输入的结点里面有的结点是无效结点,即结点不在链表上,排序后需要将这些有效结点放到左端,无效结点放到后面):
 

bool cmp(node a,node b){
	if(a.isvalid==false||b.isvalid==false){//至少有一个结点是无效结点时; 
		return a.isvalid>b.isvalid; //true的值是1,false的值是0,所以就有>让true值在左边; 
	}else {//两个都是有效结点时;
		return a.data<b.data;//按题目要求排序; 
	} 
}

二,一定要注意筛选有效结点和无效结点,并不是输入的所有结点都是有效结点

代码:
 

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct Node{
	int address;
	int data;
	int next=-1;//链表结点没有连接之前指向NULL; 
	bool isvalid=false;
}node[maxn];
bool cmp(Node a,Node b){
	if(a.isvalid==false||b.isvalid==false){
		return a.isvalid>b.isvalid;//true=1,false=0,这样就可以把true的放在前面,即有效结点放数组前; 
	}else {//两个都是有效结点时,就需要根据题目要求data从小到大排序 
		return a.data<b.data;
	}
}
int main(){
	int n,head;//地址在5位数内,所以可以用静态链表;
	cin>>n>>head;
	int id,data,next;
	for(int i=0;i<n;i++){//创建静态链表结点; 
		cin>>id>>data>>next;
		node[id].data=data;
		node[id].next=next;
		node[id].address=id;//记录其地址,排序后,其下标不再代表其地址; 
	} 
	int validnode=0;
	for(int p=head;p!=-1;p=node[p].next){
		node[p].isvalid=true;//为后面排序作准备; 
		validnode++;
	}
	sort(node,node+maxn,cmp);
	for(int i=0;i<validnode-1;i++){
		node[i].next=node[i+1].address;//排序以后应该改变其next,让结点重新指向,即创造排序后的新链表;
	}
		node[validnode-1].next=-1;
	if(validnode==0)cout<<"0"<<" "<<"-1";//特例,当链表中没有有效结点时; 
	else {
		printf("%d %05d\n",validnode,node[0].address);
		for(int i=0;i<validnode;i++){
		if(i==validnode-1)
		printf("%05d %d -1\n",node[i].address,node[i].data,node[i].next);//最后一个结点指向-1而不能是以%05d,出来的是-0001不是-1,所以最后一个输出要作特殊输出; 
		else 
		printf("%05d %d %05d\n",node[i].address,node[i].data,node[i].next);
		}	
	}
	return 0;
}

三,要注意最后一个结点的处理,不能用%05d输出来,最后一个结点的next域=-1,如果用%05d输出的话就是-0001而不是-1,所以要注意最后一个结点的next域的处理;

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值