1052. Linked List Sorting (25)-PAT

1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard

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
推荐指数:※※
来源: http://pat.zju.edu.cn/contests/pat-a-practise/1052
这道题对于key排序是显而易见的。要注意input输入的点,不一定都在list上。题目中” total number of nodes in memory“不是total number of nodes in list.不过建议出题的大神将”where N is the total number of nodes in the list “将其中的N 改为M 会让题目意思更为清晰。
如果题目意思清楚了,那就使用father list array去记录list,就可以了。(不要暴力查找了,会超时)
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<string.h>
using  namespace std;
#define  N 8
#define  MAX_LINKS 100000
typedef struct link_node{
	int  addr;
	int key;
	int next;
}link_node;
int compare_key(const void *a ,const void *b){
	if(((link_node *)a)->key<((link_node *)b)->key)
		return -1;
	else if(((link_node *)a)->key==((link_node *)b)->key)
		return 0;
	else
		return 1;
}
int main()
{
	int n,i,j,flag, first_node;
	int ft[MAX_LINKS][2];
	cin>>n>>first_node;
	link_node *links=new link_node[n];
	for(i=0;i<n;i++){
		cin>>links[i].addr>>links[i].key>>links[i].next;
		ft[links[i].addr][0]=links[i].next;//father link
		ft[links[i].addr][1]=0;//init
	}

	flag=0;
	while(first_node!=-1){//find node in link
		ft[first_node][1]=1;
		first_node=ft[first_node][0];
		flag++;
	}
	qsort(links,n,sizeof(link_node),compare_key);
	int count=0;
	if(flag!=0){
		bool headprint=0;
		for(i=0;i<n;i++){
			if(1==ft[links[i].addr][1]){
				if(0==headprint){
					printf("%d %05d\n",flag,links[i].addr);
					headprint=1;
					printf("%05d %d",links[i].addr,links[i].key);
					count++;
				}
				else{
					printf(" %05d\n%05d %d",links[i].addr,links[i].addr,links[i].key);
					count++;
				}
				if(count>=flag)
					break;
			}
		}
		printf(" -1\n");
	}
	else
		cout<<0<<" "<<-1<<endl;
	return 0;
		
}



你可能感兴趣的:(1052. Linked List Sorting (25)-PAT)