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;
    }
    
    
  • 相关阅读:
    连接服务器时通时不通的问题
    二进制包搭建k8s集群
    2、带着问题学习JVM_对象存活_垃圾回收的内容_理论_收集算法
    四、redis事务_真正实现分布式Lua_分布式锁
    四、Spring 框架中接入单机Redis的两种方式
    3、通过zkCli⼯具命令和zk 的java API两种方式创建⼀个主从模式示列
    1、带着问题学习JVM_Java为什么属于编译型+解释型的高级语言_Class类文件结构理解
    2、zookeeper安装、常用命令、API说明及简单示列(创建ZK会话及对节点进行操作、创建分布式锁、创建全局唯一ID)
    1、zk的产生、优劣、基础知识
    十三、Spring容器的原理及源码分析
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327575.html
Copyright © 2011-2022 走看看