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;
    }
    
    
  • 相关阅读:
    关于tomcat启动时报错Address already in use: JVM_Bind
    matlab学习笔记第四章——统计和MATLAB编程介绍
    matlab学习笔记第三章——绘图与图形
    matlab学习笔记第二章——矩阵
    matlab学习笔记第一章
    机器学习 什么是监督学习和无监督学习
    matlab M文件和面向对象编程
    matlab 字符串、元胞和构架数组
    使用VMware搭建3台一模一样的Linux虚拟机 搭建hadoop集群环境
    Hadoop之单机模式环境搭建
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327575.html
Copyright © 2011-2022 走看看