PAT 1052 Linked List Sorting (25 分)

1052 Linked List Sorting (25 分)

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 NNN (<105< 10^5<105) and an address of the head node, where NNN 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-11.

Then NNN 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-10^5, 10^5105,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 NNN 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

代码:

#include <cstdio> 
#include <algorithm>
using namespace std;
//记得增加头文件和命名空间
 
const int maxn = 100010;

struct NODE{
	int key ;
	int next ;
	int address;	//不要依赖数组下标,因为要重新排序 
	int flag ;		//标记是否为有效节点 
}node[maxn];

bool cmp(NODE a,NODE b){
	//自定义sort中的cmp函数实现二级比较 
	if(a.flag == -1 || b.flag == -1){
		return a.flag > b.flag ;
	}else{
		return a.key < b.key;
	}
}

int main(){
	int n,head;
	for(int i = 0 ; i < maxn ; i++){
		//对结构体中参数进行初始化
		//不可以在结构体定义时复制,Warning!  
		node[i].flag = -1; 
	} 
	scanf("%d %d",&n,&head);
	
	int address,key,next;
	
	for(int i = 0 ; i < n ; i++){
		scanf("%d %d %d",&address,&key,&next);	
		node[address].address = address;
		node[address].key = key;
		node[address].next = next;
	//	node[address].flag = 1;
	}
	
	int count = 0,p = head;
	//遍历链表中的有效结点,筛除那些无效节点 
	while(p != -1){
		node[p].flag = 1;
		count ++;
		p = node[p].next;
	}
	//考虑到一个有效结点都没有 
	if(count == 0){
		printf("0 -1");
	}else{
		sort(node , node + maxn ,cmp);
		printf("%d %05d\n" , count , node[0].address);
		
		for(int i = 0 ; i < count ; i++){
			if(i != count -1){
				printf("%05d %d %05d\n" , node[i].address , node[i].key , node[i+1].address);
			}else{
				printf("%05d %d -1",node[i].address , node[i].key );
	
			}
		}
	}
	
	return 0 ;
}

总结:

  1. 可以直接使用%05d的输出格式,以在不足五位时在高位补 0。但是要注意-1不能使用%05d输出,否则会输出-0001(而不是-1或者-00001),因此必须要留意-1的输出。
  2. 题目可能会有无效结点,即不在题目给出的首地址开始的链表上。(要记住!不要忘记可能存在无效结点鸭!!!)
  3. 数据里面还有均为无效的情况,这时就要根据有效结点的个数特判输出"0 -1"(不看答案根本想不到这一点)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值