PAT A1052 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.

链表包含一系列结构体,这些结构体在内存中并不需要连续存储,我们假设每一个结构体包含一个整数key和一个指向下一个结构体的指针Next。现在给你一个链表,你需要按照他们key值大小进行递增排序。

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 [−10​^5​​,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.

每一个输入文件都包含着一个测试用例。每一个测试用例中,第一行包含一个正数N(<10^5)和头结点的地址,N是内存中的节点总数,每个节点的地址都是一个5位正整数,NULL用-1表示。随后有N行,每一行都描述了一个节点信息,格式如下 

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.

对于每个测试用例,输出格式与输入格式相同,其中n是列表中节点的总数,所有节点都必须按顺序排序。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100010;
struct Node{
	int address;
	int key;
	int next;
	bool flag;
}node[maxn];

bool cmp(Node a,Node b){
	if(a.flag==false||b.flag==false){//无效节点排到后面
		return a.flag>b.flag;
	}else{
	 return a.key<b.key;//若都是有效节点,按key值从小到大排列
	}
}

int main(){
	for(int i=0;i<maxn;i++){
		node[i].flag=false;
	}
	
//-----------接收输入----------------------
	int n,start;
	scanf("%d %d",&n,&start);
	
	int address,key,next;
	for(int i=0;i<n;i++){
		scanf("%d %d %d",&address,&key,&next);
		node[address].key=key;
		node[address].next=next;
		node[address].address=address;
		//node[address].flag=true;
	}
//---------遍历链表,标记有效节点----------
	int count=0,p=start;
	while(p!=-1){
		node[p].flag=true; //标记有效节点
		count++; //记录有效节点数目
		p=node[p].next;
	}
	
//----------输出---------------
	if(count==0){ //若链表为空
		printf("0 -1");
	}else{
		//在sort函数里筛选有效结点,并按data从小到大排列
		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\n",node[i].address,node[i].key);
			 }
		 } 
	}
	
	return 0;
}
 

 关于sort的一般用法https://blog.csdn.net/xiangle1993/article/details/24191075

一开始我在接收输入时就把所有输入的节点flag都设为true了,并不是输入的所有节点信息都是链表中的节点。

1.接收输入信息,把节点信息存储好

2.根据起始地址遍历链表,记录有效节点个数,并标记有效节点

3.排序,用到sort函数,需自己实现排序方法cmp,无效节点放在后面,有效节点放在前面且有效节点之间按data大小排序。

4.打印结果,注意最后一个节点next为-1时,不能使用%05d输出

猜你喜欢

转载自blog.csdn.net/qq_38179583/article/details/85474898
今日推荐