zoukankan      html  css  js  c++  java
  • PAT Advanced 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 [-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

    题目分析

    已知N个节点,将节点按照值排序(升)

    解题思路

    1. 定义结构体数组,保存所有结点信息,节点中定义标记属性flag(初始化为false)
    2. 遍历所有结点信息,将出现的节点flag标记为true,并定义计数器统计链表中结点数count
    3. 遍历链表结点,修改next值为下一个结点

    易错点

    1. 题目中不容易分析出结点中有无效结点
    2. 地址格式必须为"%05d"
    3. 若结点数为0,打印"0 -1"

    知识点

    1. 将无效结点排序到最后的办法
      !a.flag || !b.flag ? a.flag > b.flag//降序true=1在前,false=0在后

    Code

    Code 01

    #include <iostream>
    #include <algorithm>
    using namespace std;
    struct NODE {
    	int address, key, next;
    	bool flag;
    } node[100000];
    int cmp1(NODE a, NODE b) {
    	return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
    }
    int main() {
    	int n, cnt = 0, s, a, b, c;
    	scanf("%d%d", &n, &s);
    	for(int i = 0; i < n; i++) {
    		scanf("%d%d%d", &a, &b, &c);
    		node[a] = {a, b, c, false};
    	}
    	for(int i = s; i != -1; i = node[i].next) {
    		node[i].flag = true;
    		cnt++;
    	}
    	if(cnt == 0) {
    		printf("0 -1");
    	} else {
    		sort(node, node + 100000, cmp1);
    		printf("%d %05d
    ", cnt, node[0].address);
    		for(int i = 0; i < cnt; i++) {
    			printf("%05d %d ", node[i].address, node[i].key);
    			if(i != cnt - 1)
    				printf("%05d
    ", node[i + 1].address);
    			else
    				printf("-1
    ");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    libjpeg的问题
    CURLcode的定义
    libcurl断点下载遇到的问题
    itunes connect
    node.js模块的坑
    itunes connect 改版后无法访问的处理办法
    cocos2d-x 在输入文字时点击语音crash
    Windows下LuaJIT的编译和使用
    多个so中模板单例的多次实例化
    多线程中避免使用信号量
  • 原文地址:https://www.cnblogs.com/houzm/p/12288439.html
Copyright © 2011-2022 走看看