ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1052 Linked List Sorting (25 分)

2021-07-13 23:02:59  阅读:183  来源: 互联网

标签:node 25 1052 Sorting 11111 list 22222 flag each


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\n");
        return 0;
    }
    printf("%d %05d\n",count,node[0].address);
    int i;
    for(i=0;i<count-1;i++){
        node[i].next=node[i+1].address;
        printf("%05d %d %05d\n",node[i].address,node[i].data,node[i].next);
    }
    printf("%05d %d %d\n",node[i].address,node[i].data,-1);
    return 0;
}

 

标签:node,25,1052,Sorting,11111,list,22222,flag,each
来源: https://www.cnblogs.com/dreamzj/p/15008837.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有