• 1052 Linked List Sorting


    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 (<) 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 −.

    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 [−], 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

    题意:

      给出一个链表,指针用地址表示,要求输出,结点的值从小到大排列后的链表。

    思路:

      模拟,注意头指针为-1的情况。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct Node {
     6     int address;
     7     int val;
     8     int next;
     9 };
    10 
    11 bool cmp(Node a, Node b) { return a.val < b.val; }
    12 
    13 int main() {
    14     int n, head;
    15     cin >> n >> head;
    16     map<int, Node> mp;
    17     for (int i = 0; i < n; ++i) {
    18         int address, val, next;
    19         cin >> address >> val >> next;
    20         mp[address] = {address, val, next};
    21     }
    22 
    23     int cur = head;
    24     vector<Node> link;
    25     while (cur != -1) {
    26         if (mp.find(cur) != mp.end()) {
    27             link.push_back(mp[cur]);
    28             cur = mp[cur].next;
    29         }
    30     }
    31     sort(link.begin(), link.end(), cmp);
    32     int len = link.size();
    33     if (len == 0)
    34         printf("0 -1
    ");
    35     else
    36         printf("%d %05d
    ", len, link[0].address);
    37     for (int i = 0; i < len - 1; ++i) {
    38         printf("%05d %d %05d
    ", link[i].address, link[i].val,
    39                link[i + 1].address);
    40     }
    41     if (len > 0)
    42         printf("%05d %d -1
    ", link[len - 1].address, link[len - 1].val);
    43     return 0;
    44 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    CentOS 7中为Yum设置代理
    在 .NET Core项目中使用UEditor图片、文件上传服务
    Android开发:通过 webview 将网页打包成安卓应用
    ElasticSearch:组合查询或复合查询
    ElasticSearch:常用的基础查询与过滤器
    SpringBoot:Java High Level REST Client 搜索 API
    SpingBoot:整合Elasticsearch7.2.0
    Linux:oracle11.2.0dbca静默建库
    Docker:跨主机通信
    Mysql无法启动情况下,如何恢复数据?
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12850007.html
Copyright © 2020-2023  润新知