zoukankan      html  css  js  c++  java
  • A1052. 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
    
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 
     6 #include <math.h>
     7 #include <algorithm>
     8 
     9 
    10 #include <string>
    11 
    12 using namespace std;
    13 const int maxn=100010;
    14 struct Node{
    15     
    16     int address,data,next;
    17     bool flag;
    18 }node[maxn];
    19 bool cmp(Node a,Node b)
    20 {
    21     if(a.flag==false||b.flag==false)
    22     {
    23         return a.flag>b.flag;
    24     } else
    25     {
    26         return a.data<b.data;
    27     }
    28 } 
    29  
    30 int main(){
    31    for(int i=0;i<maxn;i++)
    32    {
    33        node[i].flag=false;
    34    }
    35    int n,begin,address;
    36    scanf("%d %d",&n,&begin);
    37    
    38    for(int i=0;i<n;i++)
    39    {
    40        scanf("%d",&address);
    41        scanf("%d%d",&node[address].data,&node[address].next);
    42        node[address].address=address;
    43    }
    44   int count=0,p=begin;
    45   while(p!=-1)
    46   {
    47       node[p].flag=true;
    48       count++;
    49       p=node[p].next;
    50   }
    51   if(count==0)
    52   {
    53       printf("0 -1
    ");
    54   }else
    55   {
    56       sort(node,node+maxn,cmp);
    57       printf("%d %05d
    ",count,node[0].address);
    58       for(int i=0;i<count;i++)
    59       {
    60           if(i!=count-1)
    61           {
    62               printf("%05d %d %05d
    ",node[i].address,node[i].data,node[i+1].address);
    63           }else
    64           {
    65               printf("%05d %d -1
    ",node[i].address,node[i].data);
    66           }
    67       }
    68   }
    69     return 0;
    70 }
  • 相关阅读:
    正则表达式 常见的简写形式
    Git 常用命令
    利用npm安装删除模块
    发送验证码设置settime(验证码倒计时)
    30分钟后过期(订单过期)
    JS 获取当前日期时间以及其他操作
    判断终端
    一个轻量、可拓展、针对手机网页的前端开发者调试面板vConsole
    数据结构与算法----树(中)
    数据结构与算法----树(上)
  • 原文地址:https://www.cnblogs.com/ligen/p/4312251.html
Copyright © 2011-2022 走看看