zoukankan      html  css  js  c++  java
  • 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 (<) 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 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct Node
     6 {
     7     string s, ends;
     8     int val;
     9     friend bool operator < (const Node &a, const Node &b){
    10         return a.val < b.val;
    11     }
    12 }node[100005],ans[100005];
    13 map<string, int> mp;
    14 int n;
    15 string ss;
    16 int main(){
    17     cin >> n >> ss;
    18     for(int i = 0; i < n; i++){
    19         cin >> node[i].s >> node[i].val >> node[i].ends;
    20         mp[node[i].s] = i;
    21     }
    22     int p = 0;
    23     while(ss != "-1"){
    24         int a = mp[ss];
    25         ans[p++] = node[a];
    26         ss = node[a].ends;
    27     }
    28     if(p == 0){
    29         cout <<"0 -1"<<endl;
    30         return 0;
    31     }
    32     sort(ans, ans+p);
    33     cout << p << " " << ans[0].s << endl;
    34     for(int i = 0; i < p-1; i++){
    35         cout << ans[i].s <<" "<<ans[i].val << " "<<ans[i+1].s<<endl;
    36     }
    37     cout << ans[p-1].s <<" "<<ans[p-1].val << " "<<"-1"<<endl;
    38     return 0;
    39 }


  • 相关阅读:
    生成TXT下载并以逗号分隔
    使用javascript绑定键盘enter事件到asp.net的button控件 .
    学习wpf播放视频音频的两种不同方法
    批量修改数据库表的架构sql
    如何修改Sql2005注册服务器名称
    虚拟化之Hypervisor
    JAVA环境配置
    centos系统网卡配置详解
    Kali Linux安装
    Linux扩容新增磁盘分区挂载fdisk
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11185665.html
Copyright © 2011-2022 走看看