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 (<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
    思路:
      不是所有结点都在链表上的,所以需要遍历再排序。
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<set>
    #include<stack>
    #include<string.h>
    #include<cstdio>
    #include <unordered_map>
    #include<cmath>
    using namespace std;
    
    
    struct Node
    {
        long long int key;
        string address;
        string next;
    };
    bool cmp(Node&A,Node&B)
    {
        return A.key<B.key;
    }
    
    int main()
    {
        int n;
        string head;
       // cin>>n>>head;
        char tep[10];
        scanf("%d%s",&n,tep);
        head=string(tep);
        unordered_map<string,Node>mp;
        Node temp;
        for(int i=0;i<n;i++)
        {
            char ch1[10],ch2[10];
            scanf("%s%lld%s",ch1,&temp.key,ch2);
            temp.address=string(ch1);
            temp.next=string(ch2);
            mp[temp.address]=temp;
        }
       // string address=head;
       if(head=="-1")
       {
           printf("0 -1
    ");
           return 0;
       }
        vector<Node> node;
        temp=mp[head];
        node.push_back(temp);
        while(temp.next!="-1")
        {
            temp=mp[temp.next];
            node.push_back(temp);
        }
        sort(node.begin(),node.end(),cmp);
        head=node[0].address;
        printf("%d %s
    ",node.size(),head.c_str());
        for(int i=0;i<node.size()-1;i++)
        {
            node[i].next=node[i+1].address;
            printf("%s %lld %s
    ",node[i].address.c_str(),node[i].key,node[i].next.c_str());
        }
        node[node.size()-1].next="-1";
        printf("%s %lld %s
    ",node[node.size()-1].address.c_str(),node[node.size()-1].key,node[node.size()-1].next.c_str());
    
        return 0;
    }
    
    
  • 相关阅读:
    iOS 日历控件
    iOS签发者无效
    Swift3
    GitHub管理代码-随笔
    iOS10 适配问题-Xcode8
    iOS 性能调试
    bug
    贝塞尔曲线(UIBezierPath)属性、方法汇总
    webView 自适应高度 document.body 属性
    iOS-集成支付宝支付、微信支付简单总结
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327575.html
Copyright © 2011-2022 走看看