【通过PAT复习C++与数据结构】PAT-A 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 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 [−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.

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

大意

对链表元素从大到小排列输出。

直觉

感觉很简单,虽然说是用链表,但是实际上我们用结构体数组存放就行了,然后对数组排序可以直接用sort。
排完之后再修改next域就好。

解题思路

直觉的解法总体上是没问题的,但是提交后有几个点报错了。直觉告诉我是边界情况没考虑,仔细审题后发现所给的节点不一定全在链表中的,这部分节点不应该考虑。

所以其实应该用散列表,然后遍历一次链表后,把遍历到的节点flag标志位置1。剩下节点进行排序。

如何编写sort函数:其实就是有权重的结构体排序,flag优先级高,flag1的大于flag0的,flag一样的按照value排序。

复杂度分析

O(N),因为用了散列表,时间复杂度较低,空间复杂度较高

代码

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct node{
	int address;
	int val;
	int next;
	int flag=0;
}node;
bool cmp(node a,node b){
	if(a.flag ==0||b.flag==0) return a.flag>b.flag;
	else return a.val <b.val ;
}
int main(){
	int num,first;
	cin>>num>>first;
	node input[100010];
	int address;
	for(int i=0;i<num;++i){
		cin>>address;
		input[address].address =address;
		cin>>input[address].val>>input[address].next;
	}
	int p=first;
	int total=0;
	for(int i=0;i<num&&p!=-1;++i){
		input[p].flag =1;
		p=input[p].next ;
		++total;
	}
	sort(input,input+100010,cmp);
	cout<<total<<" ";
	if(total==0){
		cout<<-1<<endl;
		return 0;
	}
	printf("%05d\n",input[0].address );
	for(int i=0;i<total;++i){
		printf("%05d %d ",input[i].address ,input[i].val);
		if(i!=total-1)printf("%05d\n",input[i+1].address) ;
		else cout<<-1;
	}


}

提交时间 状态 分数 题目 编译器 耗时 用户
2019/8/16 21:45:40
答案正确
25 1052 C++ (g++) 265 ms Chester
测试点 结果 耗时 内存
0 答案正确 10 ms 1920 KB
1 答案正确 11 ms 1888 KB
2 答案正确 11 ms 1960 KB
3 答案正确 265 ms 3752 KB
4 答案正确 10 ms 1792 KB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值