PAT Grade -1052-Linked List Sorting (+ node list traversal order)

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 (<10​5 ) 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,10​5​​], 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
analysis:

1, the sort function explanation:

Only when a, b is in the list, the only sort by keyword; otherwise, it will appear in the list of nodes on the front! ! ! (So ​​there is also an implicit priority on the inside)

int cmp(NODE a,NODE b){
	if(a.visit && b.visit)
		return a.key < b.key;
	return a.visit > b.visit;
}

2, input and output of n is different:
INPUT n are as defined in: where N is the total number of nodes in memory
ouput than the n are as defined in:where N is the total number of nodes in the list

If you do not take this into account, it may cause some use cases pass.Thus the number of nodes to use the variable num recorded actually exists in the list of, Rather than the number of nodes in memory ( as part of the memory nodes is discrete and can not be seen as a node in the linked list ).

3, pay attention to consider the case where n is 0! ! ! Otherwise, some use cases pass.

code show as below

#include<iostream>
#include<algorithm>
using namespace std;
struct NODE{
	int addr;
	int key;
	int next;
	bool visit;
}node[100000];
int cmp(NODE a,NODE b){
	if(a.visit && b.visit)
		return a.key < b.key;
	return a.visit > b.visit;
}
int main()
{
	int n,start,addr,key,next,num=0;
	scanf("%d %d", &n, &start);
	for(int i = 0;  i < n; i++){
		scanf("%d %d %d", &addr,&key,&next);
		node[addr]={addr,key,next,false};
	}
	for(int i = start; i != -1; i = node[i].next){
		node[i].visit = true;
		num++;
	}
	sort(node,node+100000,cmp);
	if(num == 0){
		printf("0 -1");
	}else{
		printf("%d %05d\n", num, node[0].addr);
	}
	for(int i = 0; i < num; i++){
		if(i == num-1){
			printf("%05d %d -1\n", node[i].addr,node[i].key);
		}else{
			printf("%05d %d %05d\n", node[i].addr,node[i].key,node[i+1].addr);
		} 
	}
	return 0;
}
发布了110 篇原创文章 · 获赞 746 · 访问量 3万+

Guess you like

Origin blog.csdn.net/qq_42437577/article/details/104184016