前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 1052 Linked List Sorting (25分) 结构体排序而已

PAT 1052 Linked List Sorting (25分) 结构体排序而已

作者头像
vivi
发布2020-07-14 10:59:00
3180
发布2020-07-14 10:59:00
举报
文章被收录于专栏:vblogvblog

题目

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:

代码语言: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

解析

题目:

  • 给出n个节点的地址、key值、下一个节点的地址;给出链表的首节点的地址
  • 要求将链表中的节点按照key值从小到大排序,再按顺序输出每个节点的地址、key值、下个节点的地址

思路:

  • 用结构体数组Node[100000]存储给出的所有节点,其中flag是用来标记这个节点是否是链表中的节点。
代码语言:javascript
复制
	struct Node{
	    int addr, key, next;
	    bool flag;
	}node[100000];
  • 从全部节点中,找出组成链表的那些节点,标记他们的flagtrue,并统计节点个数。
代码语言:javascript
复制
	for (int i = head; i != -1; i = node[i].next) {
        // 标记
        node[i].flag = true;
        // 统计个数
        cnt++;
    }
  • node[]按要求排序,排序后前cnt个节点就是按key值排序好的链表。
代码语言:javascript
复制
	// 若是链表中的节点,按照key值排序
	// 若不是,则 链表节点在前,非列表节点在后
	int cmp(Node a, Node b) {
	    return a.flag && b.flag ? a.key < b.key : a.flag > b.flag;
	}
	sort(node, node + 100000, cmp);
  • 按输出模式逐个输出前cnt个节点即可。
代码语言:javascript
复制
	    for(int i = 0; i < cnt; i++) {
            printf("%05d %d ", node[i].addr, node[i].key);
            if(i != cnt - 1)
                printf("%05d\n", node[i + 1].addr);
            // 最后一个节点
            else
                printf("-1\n");
        }  

注意:

  • 节点的地址是五位数字,因此输出时必须指定格式 printf("%05d", addr)
  • 考虑cnt=0的特殊情况,也就是给出的所有节点并未组成一个链表,此时应该输出
代码语言:javascript
复制
"0 -1"

代码

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
using namespace std;
// 存储链表节点
struct Node{
    int addr, key, next;
    bool flag;
}node[100000];

// 若是链表中的节点,按照key值排序
// 若不是,则 链表节点在前,非列表节点在后
int cmp(Node a, Node b) {
    return a.flag && b.flag ? a.key < b.key : a.flag > b.flag;
}

int main() {
    int n, cnt = 0, head, addr, key, next;
    // n个节点,head是第一个节点的地址
    cin >> n >> head;
    while (n-- > 0) {
        // 每个节点的地址、key、下一个节点的地址
        cin >> addr >> key >> next;
        node[addr] = {addr, key, next, false};
    }
    // 从给出的所有节点中找到一条链表
    for (int i = head; i != -1; i = node[i].next) {
        // 标记
        node[i].flag = true;
        // 统计个数
        cnt++;
    }
    // 这些节点并未组成一个链表,相互无关
    if (cnt == 0) {
        cout << "0 -1";
    } else {
        // 按key值排序
        sort(node, node + 100000, cmp);
        // 输出节点个数和首节点地址
        printf("%d %05d\n", cnt, node[0].addr);
        // 排序后,按顺序输出
        for(int i = 0; i < cnt; i++) {
            printf("%05d %d ", node[i].addr, node[i].key);
            if(i != cnt - 1)
                printf("%05d\n", node[i + 1].addr);
            // 最后一个节点
            else
                printf("-1\n");
        }          
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 解析
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档