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;
    }
    
    
  • 相关阅读:
    奇妙的 CSS shapes(CSS图形)
    正确的缩写document。querySelector
    Ajax异步获取html数据中包含js方法无效的解决方法
    关于前端开发中的“收口”思想
    说说JSON和JSONP,也许你会豁然开朗
    Ajax 完整教程(转载)
    GitHub与Git指令入门
    Vue.js——60分钟组件快速入门(下篇)
    Vue.js——60分钟组件快速入门(上篇)
    自定义构造函数
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327575.html
Copyright © 2011-2022 走看看