PAT|1052 Linked List Sorting(链表、排序、结构体)

题目大意

链表由一系列结构组成,这些结构在内存中不一定相邻。我们假设每个结构都包含一个整数键和一个指向下一个结构的Next指针。现在给出了一个链表,您应该按照其键值按升序对结构进行排序。

做题分析

  1. 本来以为只要控制好节点自己的地址和值即可,因此直接使用了map排序解决的问题。最后提交才发现部分用例无法通过。看了柳神的题解才知道有的节点根本不在链表之中。
    在这里插入图片描述
  • 给大家看看错误代码,引以为戒。
#include<bits/stdc++.h>
using namespace std;
int numNode, pre, value, nex;
map<int, int> myMap;
int main() {
	cin >> numNode >> pre;
	for (int i = 0; i < numNode; i++) {
		cin >> pre >> value >> nex;
		myMap.emplace(value, pre);
	}

	map<int, int>::iterator iter = myMap.begin();
	cout << numNode << ' ';
	while (iter != myMap.end()) {
		printf("%05d\n", iter->second);
		printf("%05d %d ", iter->second, iter->first);
		iter++;
	}
	cout << "-1";

	return 0;
}
  • 改完以后竟然只通过了一个用例。呜呜呜 - 在这里插入图片描述

代码如下:

#include<bits/stdc++.h>
using namespace std;
struct MyStruct
{
	int value;
	int pre;
	int nex;
	bool flag = 0;
};
int numNode;
map<int, int> myMap;
vector<MyStruct> vecNode(100000);
int main() {
	int head,pre;	
	cin >> numNode >> head;
	for (int i = 0; i < numNode; i++) {
		cin >> pre;
		cin >> vecNode[pre].value >> vecNode[pre].nex;
	}

	//遍历链表、找到位于
	for (int i = head; i != -1; i = vecNode[i].nex ) {
		myMap.emplace(vecNode[i].value, i);
	}

	if (myMap.size() == 0) {
		cout << "0 -1";
	}
	else {

		map<int, int>::iterator iter = myMap.begin();
		cout << numNode << ' ';
		while (iter != myMap.end()) {
			printf("%05d\n", iter->second);
			printf("%05d %d ", iter->second, iter->first);
			iter++;
		}
		cout << "-1";
	}
	return 0;
}

我们再次大胆猜测,是因为使用了map数据结构,无法存储相同的两个数据。不幸的是题中说明。

It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

  • 问题终于找到了
    竟然是最后的数量没有更新(删除多余节点)
	map<int, int>::iterator iter = myMap.begin();
		cout << myMap.size() << ' ';

最后AC

在这里插入图片描述

最终代码

#include<bits/stdc++.h>
using namespace std;
struct MyStruct
{
	int value;
	int pre;
	int nex;
};
int numNode;
map<int, int> myMap;
vector<MyStruct> vecNode(100000);
int main() {
	int head,pre;	
	cin >> numNode >> head;
	for (int i = 0; i < numNode; i++) {
		cin >> pre;
		vecNode[pre].pre = pre;
		cin >> vecNode[pre].value >> vecNode[pre].nex;
	}
	//遍历链表、找到位于
	for (int i = head; i != -1; i = vecNode[i].nex ) {
		myMap.emplace(vecNode[i].value, vecNode[i].pre);
	}
	if (myMap.size() == 0) 
		cout << "0 -1";
	else {
		map<int, int>::iterator iter = myMap.begin();
		cout << myMap.size() << ' ';
		while (iter != myMap.end()) {
			printf("%05d\n", iter->second);
			printf("%05d %d ", iter->second, iter->first);
			iter++;
		}
		cout << "-1" << endl;
	}
	return 0;
}

题目总结

  • 原来题目真的会哪里都有坑,刚开始没有ac的两个用例,最后一个是因为如果是0的话没控制好,第二个是出现了部分节点孤立的情况。
  • 首先定义结构体存储节点信息。地址作为数据下表,记录信息。
  • 遍历链表,确定在链上的节点,利用map自动排序的性质,对对应的节点进行排序。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值