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常用互动方法
    JS初级学习准备
    IntelliJ IDEA编辑器激活码
    百度个性化地图的使用
    input输入框 只能输入数字 oninput = "value=value.replace(/[^d]/g,'')" input输入框输入大于0的数字 oninput="value=value.replace(/D|^0/g,'')"
    vue中父子组件传值问题 通过props 和 $emit()方法
    Do not use built-in or reserved HTML elements as component id:mask vue报错
    C++ vector的越界错误
    合并K个有序链表
    合并两个有序数组
  • 原文地址:https://www.cnblogs.com/549294286/p/3574299.html
Copyright © 2011-2022 走看看