本文由参考于柳神博客写成
柳神的博客,这个可以搜索文章
柳神的个人博客,这个没有广告,但是不能搜索
还有就是非常非常有用的 算法笔记 全名是
算法笔记 上级训练实战指南 //这本都是PTA的题解 算法笔记PS 今天也要加油鸭
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.
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 Nextwhere 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.
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.
Linked 把什么连接起来 有联系
structures 结构
adjacent 临近的
给你几个node让你把他们按照key的值按顺序进行排列
一个sort解决问题
这个链表可能是空的,在空链表的情况下.
我们要输出
0 -1柳神的代码比我的代码好在只用了一个结构体.
用了一个bool类型来帮助写移动代码.
#include <iostream> #include <algorithm> using namespace std; struct NODE { int address, key, next; bool flag; }node[100000]; int cmp1(NODE a, NODE b) { return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key; } int main() { int n, cnt = 0, s, a, b, c; scanf("%d%d", &n, &s); for(int i = 0; i < n; i++) { scanf("%d%d%d", &a, &b, &c); node[a] = {a, b, c, false}; } for(int i = s; i != -1; i = node[i].next) { node[i].flag = true; cnt++; } if(cnt == 0) { printf("0 -1"); } else { sort(node, node + 100000, cmp1); printf("%d d\n", cnt, node[0].address); for(int i = 0; i < cnt; i++) { printf("d %d ", node[i].address, node[i].key); if(i != cnt - 1) printf("d\n", node[i + 1].address); else printf("-1\n"); } } return 0; }如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.
