zoukankan      html  css  js  c++  java
  • 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
     题解:使用静态链表,将地址保存在节点中,遍历链表进行标记(排除掉非链表节点),排序输出
     
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1000010;
    #define inf 0x3fffffff
    struct Node{
        int address,data,next;
        bool flag;
    }node[maxn];
    bool cmp(Node a,Node b){
        if(a.flag==false||b.flag==false){
            return a.flag>b.flag;
        }
        else if(a.data!=b.data){
            return a.data<b.data;
        }
    }
    int main(){
        int n,address,begin;
        scanf("%d %d",&n,&begin);
        int data,next,count=0;
        for(int i=0;i<n;i++){
            scanf("%d %d %d",&address,&data,&next);
            node[address].address=address;
            node[address].data=data;
            node[address].next=next;
        }
        int p=begin;
        while(p!=-1){
            count++;
            node[p].flag=true;
            p=node[p].next;
            
        }
        sort(node,node+maxn,cmp);
        if(count==0){
            printf("0 -1
    ");
            return 0;
        }
        printf("%d %05d
    ",count,node[0].address);
        int i;
        for(i=0;i<count-1;i++){
            node[i].next=node[i+1].address;
            printf("%05d %d %05d
    ",node[i].address,node[i].data,node[i].next);
        }
        printf("%05d %d %d
    ",node[i].address,node[i].data,-1);
        return 0;
    }
  • 相关阅读:
    分布式系统与机器学习相关笔记
    天坑:OpenGL制作游戏引擎备忘记录
    2016ACM-ICPC 大连站、青岛站、China-Final 赛后总结(无删版)by wanglangzhe || wmzksana
    CF 609E, 树链剖分
    CF 609F,线段树上二分 + set维护
    CF 540E, 树状数组
    hdu 5726, 2016多校1
    hdu5836, 2016CCPC网络赛
    SYSU-10,URAL 1675,容斥原理
    动态规划基础篇 from 51nod
  • 原文地址:https://www.cnblogs.com/dreamzj/p/15008837.html
Copyright © 2011-2022 走看看