PAT 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 [−10​5​ ,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

解析

题目:

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

思路:

  • 用结构体数组Node[100000]存储给出的所有节点,其中flag是用来标记这个节点是否是链表中的节点。
	struct Node{
     
	    int addr, key, next;
	    bool flag;
	}node[100000];
  • 从全部节点中,找出组成链表的那些节点,标记他们的flagtrue,并统计节点个数。
	for (int i = head; i != -1; i = node[i].next) {
     
        // 标记
        node[i].flag = true;
        // 统计个数
        cnt++;
    }
  • node[]按要求排序,排序后前cnt个节点就是按key值排序好的链表。
	// 若是链表中的节点,按照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个节点即可。
	    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的特殊情况,也就是给出的所有节点并未组成一个链表,此时应该输出
"0 -1"

代码

#include 
#include 
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;
}

你可能感兴趣的:(c/c++,algorithm,PAT,链表,算法,数据结构,c++)