EchoDemo's Blogs

PAT A1052 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

题目大意:给出一个静态链表,将链表按照数据域data的值从小到大进行排序,然后把排序后的静态链表上的结点从头结点顺序输出。

分析:建立存储静态节点的结构体数组,从首节点开始的顺序(直到-1)遍历整个静态链表,将在静态链表中节点的flag标记为true,并且统计count(有效结点的个数)大小,这里是因为有的你输入的节点根本不在静态链表上。之后将静态链表进行排序,这里需要进行二次排序:如果两个节点中有一个节点的标志位为false就把它移动到后面(reuturn a.flag>b.flag),否则就按照节点数据域从小到大进行排序(return a.data<b.data)。最后只输出前count个静态链表节点。

#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;

const int maxn = 100010;

struct Node {
    int address;
    int data;
    int next;
    bool flag;
}node[maxn];

bool cmp(Node a, Node b) {
    if (a.flag == false || b.flag == false) {
        return a.flag > b.flag;//将无效节点放到后面
    }
    else {
        return a.data < b.data;//按照数据域的顺序从大到小排列
    }
}

int main() {
    int n, x;
    cin >> n >> x;
    int address, data, next;
    for (int i = 0;i<n;i++) {//初始化静态链表
        cin >> address >> data >> next;
        node[address].address = address;
        node[address].data = data;
        node[address].next = next;
    }

    for (int i = 0;i < maxn;i++) {
        node[i].flag = false;//初始化标志位
    }
    int count = 0;
    while (x != -1) {//遍历静态链表,对有效的节点进行标记,同时统计有效节点的个数(因为存在一些你输入的节点但是不在链表上情况)
        node[x].flag = true;
        count++;
        x = node[x].next;
    }
    if (count == 0) {//如果头结点为-1,即链表中没有节点
        cout << "0 -1" << endl;
    }
    else {
        sort(node, node + maxn, cmp);//筛选有效节点,按data从小到大排列
        printf("%d %05d\n", count, node[0].address);

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

    }
    system("pause");
    return 0;
}
🐶 您的支持将鼓励我继续创作 🐶
-------------本文结束感谢您的阅读-------------