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;
    }

     

  • 相关阅读:
    Orleans is a framework
    修改emlog后台登录路径的方法(转)
    form表单中的 action=./?> 是什么意思
    10 个迅速提升你 Git 水平的提示(转)
    为什么国外程序员爱用苹果Mac电脑?(转)
    Socket 专题
    Android 时间戳简单转化
    Android 常用时间格式转换代码
    Android AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
    Android AlarmManager类的应用(实现闹钟功能)
  • 原文地址:https://www.cnblogs.com/549294286/p/3574299.html
Copyright © 2011-2022 走看看