【PAT甲级】1052 Linked List Sorting (25分)

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 (<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


分析

将链表中的节点按照key值排序。

  • 用结构体存储节点,flag记录该节点是否在链表中;

    用sort方法进行排序,cmp方法:两个节点都访问过则从小到大排序,否则把没访问过的节点放后面。

  • 输入的N是内存中节点的总个数,不一定所有节点都在链表里!!输出的N是链表中节点的总个数。

1491599-20200126171620460-747117623.png


1491599-20200126171701476-94701560.png

  • 注意:节点的下址,排序后是发生变化的。
  • 注意:当链表中节点个数为0时,输出为0 -1,和其他时候不同。

  • 注意:最后一个节点的下址的输出和别的下址输出格式不同


代码

#include<iostream>
#include<algorithm>

using namespace std;

struct NODE {
    int addr,data,next;
    bool flag;//记录该节点是否在链表中
} node[100000];
int cmp1(NODE a,NODE b) {
    return a.flag&&b.flag?a.data<b.data:a.flag>b.flag;
}

int main() {
    int n,start;//n为总结点个数
    int count=0;//链表节点个数
    scanf("%d %d",&n,&start);

    //存放数据
    int addr,data,next;
    for(int i=0; i<n; i++) {
        scanf("%d %d %d",&addr,&data,&next);
        node[addr]= {addr,data,next,false};
    }
    //遍历链表,设置flag,计算count
    for(int i=start; i!=-1; i=node[i].next) {
        node[i].flag=true;
        count++;
    }

    if(count==0) {
        printf("0 -1");
    } else {
        //排序
        sort(node,node+100000,cmp1);

        //输出
        printf("%d %05d\n",count,node[0].addr);
        for(int i=0; i<count; i++) {
            printf("%05d %d ",node[i].addr,node[i].data);
            if(i==count-1)
                printf("-1\n");
            else
                printf("%05d\n",node[i+1].addr);
        }
    }
    return 0;
}


问题

  • count变量放在开头定义和就近定义的答案不一样;int默认值是0,但我打印了一下count输出是1,可能是堆栈数据残留。

    解决:以后变量全在开头定义,并且自己赋初值。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值