PAT 1052 Linked List Sorting

1052 Linked List Sorting

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

总结:做了这么多的结构体的题目,好不容易有一道题可以直接写出来的(用stl容器写出来的),结果你给我整这个(题目中给出的结构题不一定是链表中的节点),我真的是无语了

 自己写的代码:

#include <iostream>
#include <map>
using namespace std;

int main(){
    int n,add;
    map<int,int> m;
    scanf("%d %d",&n,&add);
    
    int addr,e,ne;
    for(int i=0;i<n;i++){
        scanf("%d%d%d",&addr,&e,&ne);
        m[e]=addr;
    }
    
    if(m.size()!=0){
        printf("%d %05d\n",m.size(),m.begin()->second);
        for(auto it=m.begin();it!=m.end();it++){
            if(it!=m.begin())   printf("%05d\n",it->second);
            printf("%05d %d ",it->second,it->first);
        }
        printf("-1");
    }
    else printf("-1 0\n");
    
    return 0;
}

大佬的代码:

这里的快速排序将整个结构体都进行了排序,由于题目只需要将结果打印出来即可,没要求说一定要将顺序的结果存储在一个结构体当中,所以结构体当中的 ne 就没起过作用

#include <iostream>
#include <algorithm>
using namespace std;

struct Node{
    int add,e,ne;
    bool flag;
}node[100010];
bool cmp(Node a,Node b){
    return !a.flag || !b.flag ? a.flag>b.flag:a.e<b.e;
}
int main(){
    int n,add;
    scanf("%d%d",&n,&add);
    
    int a,b,c,cot=0;
    for(int i=0;i<n;i++){
        scanf("%d%d%d",&a,&b,&c);
        node[a]={a,b,c,false};
    }
    for(int i=add;i!=-1;i=node[i].ne){
        node[i].flag=true;
        cot++;
    }
    if(cot==0)  printf("0 -1\n");
    else{
        sort(node,node+100010,cmp);
        printf("%d %05d\n",cot,node[0].add);
        for(int i=0;i<cot;i++){
            printf("%05d %d ",node[i].add,node[i].e);
            if(i!=cot-1)    printf("%05d\n",node[i+1].add);
            else printf("-1\n");
        }
    }
    
    return 0;
}

好好学习,天天向上!

我要考研!

2022.11.12

找了好久错误,原来是如果没有节点就直接打印 0 -1 就行了!!!

//(1)找出有效节点
//(2)将值和地址映射关系
//(3)利用alogrithm进行排序
//(好像没有解决两个重复的值,但是地址不同的问题)
//(题目说了每一个值都是不一样的)
#include <iostream>
#include <algorithm>
using namespace std;

int ne[1000000],val[1000000],add[1000010];
bool exist[1000000];
int main(){
    int n,addr;
    scanf("%d%d",&n,&addr);
    
    int x;
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        scanf("%d%d",&val[x],&ne[x]);
    }
    int cot=0;
    int minn=0x3f3f3f3f,minadr;
    int first=addr;
    //找到有效节点,计算有效节点的个数,找到排序后的第一个节点的地址
    while(first!=-1){
        cot++;
        if(val[first]<minn){
            minn=val[first];
            minadr=first;
        }
        exist[val[first]+100000]=1;//说明这个值是链表中的有效值
        add[val[first]+100000]=first;
        first=ne[first];
    }
    if(cot==0)  printf("0 -1\n");
    else{
        printf("%d %05d\n",cot,minadr);
        bool flag=false;
        for(int i=0;i<=100000*2;i++){
            if(exist[i]){
                if(flag)    printf("%05d\n",add[i]);
                printf("%05d %d ",add[i],i-100000);
                flag=true;
            }
        }
        printf("-1");
    }
    return 0;
}

//用了另一个数组存储数值,通过sort排序最后根据 顺序 的序列打印结果
//(1)找出有效节点
//(2)将值和地址映射关系
//(3)利用alogrithm进行排序
//(好像没有解决两个重复的值,但是地址不同的问题)
//(题目说了每一个值都是不一样的)
#include <iostream>
#include <algorithm>
using namespace std;

int ne[1000000],val[1000000],add[1000010];
bool exist[1000000];
int main(){
    int n,addr;
    scanf("%d%d",&n,&addr);
    int v[n];
    
    int x,idx=0;
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        scanf("%d%d",&val[x],&ne[x]);
        v[idx++]=val[x];
    }
    int cot=0;
    int minn=0x3f3f3f3f,minadr;
    int first=addr;
    //找到有效节点,计算有效节点的个数,找到排序后的第一个节点的地址
    while(first!=-1){
        cot++;
        if(val[first]<minn){
            minn=val[first];
            minadr=first;
        }
        exist[val[first]+100000]=1;//说明这个值是链表中的有效值
        add[val[first]+100000]=first;
        first=ne[first];
    }
    if(cot==0)  printf("0 -1\n");
    else{
        printf("%d %05d\n",cot,minadr);
        bool flag=false;
        sort(v,v+idx);
        for(int i=0;i<idx;i++){
            if(exist[v[i]+100000]){
                if(flag)    printf("%05d\n",add[v[i]+100000]);
                printf("%05d %d ",add[v[i]+100000],v[i]);
                flag=true;
            }
        }
        printf("-1");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值