(PAT 1052) Linked List Sorting (链表)

红太狼 2022-03-25 08:09 265阅读 0赞

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:

  1. Address Key Next

where 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.

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:

  1. 5 00001
  2. 11111 100 -1
  3. 00001 0 22222
  4. 33333 100000 11111
  5. 12345 -1 33333
  6. 22222 1000 12345

Sample Output:

  1. 5 12345
  2. 12345 -1 00001
  3. 00001 0 11111
  4. 11111 100 22222
  5. 22222 1000 33333
  6. 33333 100000 -1

解题思路:

给出一串结点,有有效结点和无效结点(在链表上的是有效结点,链表外的为无效结点),筛选出有效结点,把他们根据key的大小排序后组成新的链表,最后输出链表

根据https://blog.csdn.net/alex1997222/article/details/86553586中的模板解题即可,注意链表为空的情况

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. const int MAXN = 100010;
  5. struct Node {
  6. int key;
  7. int validation;
  8. int Addr;
  9. int next; //指向下一个结点
  10. }nodes[MAXN];
  11. bool cmp(Node a, Node b) {
  12. if (a.validation == -1 || b.validation == -1) {
  13. return a.validation > b.validation; //将有效结点左移
  14. }
  15. else {
  16. return a.key < b.key; //将数字从小到大排序
  17. }
  18. }
  19. int main() {
  20. int counter = 0;
  21. int N, headAddr;
  22. scanf("%d %d", &N, &headAddr);
  23. for (int i = 0; i < MAXN; ++i) {
  24. nodes[i].validation = -1; //先全部设置为无效结点
  25. }
  26. for (int i = 0; i < N; ++i) {
  27. int Addr, data, nextAddr;
  28. scanf("%d %d %d", &Addr, &data, &nextAddr);
  29. nodes[Addr].Addr = Addr;
  30. nodes[Addr].key = data;
  31. nodes[Addr].next = nextAddr; //指向下一个结点
  32. }
  33. //遍历链表,挑选出有效结点
  34. int p1 = headAddr;
  35. while (p1 != -1) {
  36. counter++;
  37. nodes[p1].validation = 1;
  38. p1 = nodes[p1].next;
  39. }
  40. if(counter == 0){ //链表为空的情况要特别注意
  41. printf("%d %d",counter,-1);
  42. return 0;
  43. }
  44. sort(nodes, nodes + MAXN, cmp); //排序
  45. for (int i = 0; i < MAXN; ++i) {
  46. if (nodes[i + 1].validation == -1) {
  47. nodes[i].next = -1;
  48. break;
  49. }
  50. nodes[i].next = nodes[i + 1].Addr;
  51. }
  52. printf("%d %05d\n", counter, nodes[0].Addr);
  53. for (int i = 0; i < MAXN; ++i) {
  54. if(nodes[i].validation == -1) break;
  55. if (nodes[i].next == -1) {
  56. printf("%05d %d %d\n", nodes[i].Addr, nodes[i].key, nodes[i].next);
  57. }
  58. else {
  59. printf("%05d %d %05d\n", nodes[i].Addr, nodes[i].key, nodes[i].next);
  60. }
  61. }
  62. return 0;
  63. }

发表评论

表情:
评论列表 (有 0 条评论,265人围观)

还没有评论,来说两句吧...

相关阅读

    相关 (Linked-list)

    链表(linked-list) 链表是线性表的链式存储方式,链表的内存是不连续的,链表通过一个指向下一个元素地址的引用将所有的元素串起来。 ![这里写图片描述][70