• PAT 解题报告 1052. Linked List Sorting (25)


     

    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 [-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:

    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


    题目描述:

    给一个链表按照node里面存着的key排序.

    算法分析:

    算法很简单就是排序算法, 调用现成的sort之类的库就行了, 注意两点”

    (1) 给定的node不一定都是在同一个链表上 (这就是为什么我们需要head node 的address)

    (2) head node address 可能为-1,小心segment fault.

    (3) 如果链表是空,应当输出”0 -1″.

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    #define MX 100001
    struct Node {
        int key, val, next;
    };
    //不用vector,而用数组的好处是,数组可作为hash
    Node m[MX], linked[MX];
    bool cmp(Node p, Node q) {
        return p.val<q.val;
    }
    
    int main()
    {
        int N, head;
        scanf("%d%d", &N, &head);
        for (int i=0; i<N; i++) {
            int tmp;
            scanf("%d", &tmp);
            scanf("%d%d", &m[tmp].val, &m[tmp].next);
            m[tmp].key = tmp;
        }
        int cnt=0;
        while (head != -1) {
            linked[cnt++] = m[head];
            head = m[head].next;
        }
    
        sort(linked, linked+cnt, cmp);
        if (cnt == 0) printf("0 -1");
        else {
            printf("%d %05d
    ", cnt, linked[0].key);
            for (int i=0; i<cnt;i++) {
                if (i!=cnt-1) {
                    printf("%05d %d %05d
    ", linked[i].key, linked[i].val, linked[i+1].key);
                }
                else {
                    printf("%05d %d -1", linked[i].key, linked[i].val);
                }
            }
        }
    
        return 0;
    }

     

  • 相关阅读:
    JS高阶---继承模式(原型链继承)
    一切皆数据,人生需学习
    操作系统升级
    看了跨年演讲后
    新的开始
    怎么做一名高薪前端工程师,必备哪些技术工具?
    web服务器 Nginx
    实用的软件架构方法
    购买网络自动化工具时应考虑的7个方面
    ES6 展开操作符的几个妙用
  • 原文地址:https://www.cnblogs.com/549294286/p/3574299.html
Copyright © 2020-2023  润新知