首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【PAT甲级】Linked List Sorting

【PAT甲级】Linked List Sorting

作者头像
喜欢ctrl的cxk
发布2019-11-08 14:28:51
发布2019-11-08 14:28:51
3910
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/89452300

Problem Description:

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 (<10​5​​) 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:

代码语言:javascript
复制
Address Key Next

where Address is the address of the node in memory, Key is an integer in [−10​5​​,10​5​​], 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:

代码语言:javascript
复制
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

代码语言:javascript
复制
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

解题思路:

创建一个结构体数组LinkList,其中包括当前结点的地址address、当前结点的数据data、下一个结点的地址next。按照链表结点的地址来将结点排序并依次放入vector中,用cnt来记录结点vector中的结点数量,当结点数为0时输出"0 -1"(这个测试点有1分),当结点数不为0时,根据结点数据data来对链表进行排序,然后无脑输出即可。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005

struct LinkList
{
    int address;    //当前结点的地址
    int data;       //当前结点的数据
    int next;       //下一结点的地址
}node[MAX];

bool cmp(LinkList a,LinkList b)   //根据结点数据升序排列
{
    return a.data < b.data;
}

int main()
{
    int Head,N;   //头结点Head,结点总个数N
    cin >> N >> Head;
    for(int i = 0; i < N; i++)
    {
        int temp;
        cin >> temp;
        node[temp].address = temp;
        cin >> node[temp].data >> node[temp].next; 
    }
    vector<LinkList> v;   //存放链表
    int cnt = 0;
    for(int i = Head; i != -1; i = node[i].next)  //先根据地址来将链表排序
    {
        v.push_back(node[i]);
        cnt++;
    }
    if(cnt == 0)   //当没有输入链表时
    {
        printf("0 -1\n");
    }
    else
    {
        sort(v.begin(),v.end(),cmp);   //根据链表的data升序排列
        printf("%d %05d\n",cnt,v[0].address);
        for(int i = 0; i < cnt; i++)
        {
            printf("%05d %d ",v[i].address,v[i].data);
            if(i != cnt-1)
            {
                printf("%05d\n", v[i+1].address);
            }
            else
            {
                printf("-1\n");
            }
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档