链表_A1052 Linked List Sorting (25 分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464

/*
*链表的处理
*1.定义静态链表,结构体数组
*2.初始化falg为false
*3.从链表首地址begin遍历,并标记有效结点
*4.对结点排序,有效结点true大于false
*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<algorithm>
const int MAXN=100010;

struct Node {
    int address,data,next;
    bool falg;
}node[MAXN];

bool cmp(Node a,Node b) { //函数cmp为bool类型,参数为Node
    if(a.falg==false || b.falg==false) {
        return a.falg>b.falg; //把无效结点往后放
    }else {
        return a.data<b.data;
    }
}

int main() {
    for(int i=0;i<MAXN;i++) {
        node[i].falg=false;
    }
    int num,begins,address;
    scanf("%d%d",&num,&begins);
    for(int i=0;i<num;i++) {
        scanf("%d",&address); //先读入地址
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }
    int counts=0,point=begins;
    while(point != -1) {
        node[point].falg=true;
        counts++;
        point=node[point].next;
    }
    if(counts==0) { //特判,新链表没有结点时输出0 -1
        printf("0 -1");
    }else {
        sort(node,node+MAXN,cmp);
        printf("%d %05d\n",counts,node[0].address);
        for(int i=0;i<counts;i++) {
            if(i != counts-1) {
                //结点地址node[i],node[i+1]
                printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
            }else {
                printf("%05d %d -1\n",node[i].address,node[i].data);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/2o2o/p/11371583.html

时间: 2024-12-10 13:48:57

链表_A1052 Linked List Sorting (25 分)的相关文章

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, y

【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, yo

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

题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组成.以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址. trick: 题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗... 数据点1包含有些结点并非在

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 stru

A1052 Linked List Sorting (25分)

一.技术总结 这个也是一个链表类题目,主要是在结构提上的设计,可以设计一个flag参数用于记录真实有效的结点. 然后就是根据题目要求进行排序,输出结果. cmp()函数可以,一层是把有效节点排到数组的左边,然后可以进行二次比较,按题目要求来. 二.参考代码 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct Node{ int key;//数据 int n

pat1052. Linked List Sorting (25)

1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 a

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, yo

02-线性结构3 Reversing Linked List (25 分)

02-线性结构3 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must ou

1028 List Sorting (25 分)

1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wher