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;
    }
    
    
  • 相关阅读:
    Android基于XMPP Smack Openfire下学习开发IM(六)总结
    排序数组中重复最对的数字长度
    Android之ContextMenu的使用方法以及与OptionMenu的区别
    DirectShow Filter 开发典型例子分析 ——字幕叠加 (FilterTitleOverlay)1
    javascript排序 查找算法大全
    memcpy和strlen函数的实现
    读书笔记——数据库的ADO开发总结
    一个类似“火柴棍”问题的面试题
    使用GSoap开发WebService客户端与服务端
    Java.io下的方法是对磁盘上的文件进行磁盘操作
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327575.html
Copyright © 2011-2022 走看看