zoukankan      html  css  js  c++  java
  • Linked List Sorting

    静态链表(用结构体数组模拟链表)
     
     
    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 (<) 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 −.

    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 [−], 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


    思路:
    采用结构体数组模拟链表,用结构体中的一个变量标记该元素是否出现在链表上,采用二级排序,按照值是否有效,以及数值从小到大排序,所有有效的数据均会出现在数组的左边
    一开始从st开始遍历链表获得flag和cnt等信息


     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 
     5 using namespace std ;
     6 
     7 const int N = 100010 ;
     8 
     9 struct node{
    10     int adr,data,next ;
    11     bool flag ;
    12 }arr[N];
    13 
    14 bool cmp(node &p,node &q){
    15     if(!p.flag || !q.flag){
    16         return p.flag > q.flag ;
    17     }else{
    18         return p.data < q.data ;
    19     }
    20 }
    21 
    22 int main(){
    23     int n, st ;
    24     cin >> n >> st ;
    25     for(int i=0;i<n;i++){
    26         int a,b,c ;
    27         cin >> a >> b >> c ;
    28         arr[a].adr = a ;
    29         arr[a].data = b ;
    30         arr[a].next = c ;
    31         arr[a].flag = false ;
    32     }
    33     
    34     
    35     int cnt = 0,b = st ;
    36     while(b != -1){
    37         cnt ++ ;
    38         arr[b].flag = true ;
    39         b = arr[b].next ;
    40     }
    41     
    42     sort(arr,arr+N,cmp) ;
    43     
    44     if(!cnt){
    45         cout << "0 -1" << endl ;    
    46     }else{
    47         printf("%d %05d
    ",cnt,arr[0].adr) ;
    48         for(int i=0;i<cnt;i++){
    49             if(i < cnt -1){
    50                 printf("%05d %d %05d
    ",arr[i].adr,arr[i].data,arr[i+1].adr) ;
    51             }else{
    52                 printf("%05d %d -1
    ",arr[i].adr,arr[i].data) ;
    53             }
    54         }
    55     }
    56     
    57     return 0 ;
    58 }

    ...

  • 相关阅读:
    MapReduce in MongoDB
    MongoDB的一些基本操作
    谈谈NOSQL
    Java中的反射(1)
    Mybatisの常见面试题
    关于Lombok和自动生成get set方法
    订Pizza(Java)
    美化Div的边框
    爱,死亡和机器人(Love,Death&Robots)
    session与cookie的介绍和两者的区别之其相互的关系
  • 原文地址:https://www.cnblogs.com/gulangyuzzz/p/12077050.html
Copyright © 2011-2022 走看看