zoukankan      html  css  js  c++  java
  • 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 实现两个小数的相乘、相除功能
    echarts图表初始大小问题及echarts随窗口变化自适应
    element-ui走马灯如何实现图片自适应 长度和高度 自适应屏幕大小
    vue中淡入淡出示例
    CSS3------box-shadow,即单边阴影效果设置
    webpack4 自学笔记五(tree-shaking)
    webpack4 自学笔记四(style-loader)
    webpack4 自学笔记三(提取公用代码)
    webpack4 自学笔记二(typescript的配置)
  • 原文地址:https://www.cnblogs.com/549294286/p/3574299.html
Copyright © 2011-2022 走看看