zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    【WCF学习随笔三】初见WCF。
    【WCF学习随笔二】第一个WebService应用。
    【WCF学习随笔一】序言。
    .NET Framework 4.5.1 开源了是广大程序员的巨大财富。
    数据逆向传递 unwind segue
    segue生命周期
    FusionChart学习笔记(部分)
    图解MonoForAndroid开发环境搭建
    jdbc_odbc SQLserver 驱动安装及测试
    关键词:CodeSmith工具、Money类型、__UNKNOWN__
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12850007.html
Copyright © 2011-2022 走看看