• PAT甲级——A1074 Reversing Linked List


    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 output 4→3→2→1→5→6.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤) which is the total number of nodes, and a positive K (≤) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

    Then N lines follow, each describes a node in the format:

    Address Data Next
    

    where Address is the position of the node, Data is an integer, and Next is the position of the next node.

    Output Specification:

    For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

    Sample Input:

    00100 6 4
    00000 4 99999
    00100 1 12309
    68237 6 -1
    33218 3 00000
    99999 5 68237
    12309 2 33218
    

    Sample Output:

    00000 4 33218
    33218 3 12309
    12309 2 00100
    00100 1 99999
    99999 5 68237
    68237 6 -1


    算法设计:
    由于所给的地址是5位非负整数,可以定义两个维度为100005的一维数组data、Next,负责储存数据域和下一个地址
    定义一个vector<int>listAddress,由所给链表开始地址处开始遍历整个链表,按遍历顺序将各结点地址储存到listAddress中。
    按要求对listAddress数组进行翻转,可以利用c语言库里的reverse函数

    按格式要求进行结果输出

    注意点:

    (1)题目给出的结点中可能有不在链表中的无效结点

    (2)翻转时要注意如果最后一组要翻转的结点数量小于K,则不进行翻转;如果等于K,需要进行翻转

    (3)输出时结点地址除-1外要有5位数字,不够则在高位补0 。所以地址-1要进行特判输出

     1 //s首先说明一下PAT的常见陷阱,就是给出的数据未必是一条链表,可能是多条,
     2 //所以首先从输入的数据的中找到那条链表
     3 //巨简单,使用vector反转就行了
     4 #include <iostream>
     5 #include <vector>
     6 #include <algorithm>
     7 using namespace std;
     8 int head, N, K;
     9 int nodes[100100], nexts[100100];
    10 vector<int>list;
    11 int main()
    12 {
    13     cin >> head >> N >> K;
    14     int adrr, data, next, temp = head;
    15     for (int i = 0; i < N; ++i)
    16     {
    17         cin >> adrr >> data >> next;
    18         nodes[adrr] = data;
    19         nexts[adrr] = next;
    20     }
    21     while (temp != -1)//找出这条链表
    22     {
    23         list.push_back(temp);
    24         temp = nexts[temp];
    25     }
    26     for (int i = K; i <= list.size(); i += K)
    27         reverse(list.begin() + i - K, list.begin() + i);
    28     for (int i = 0; i < list.size(); ++i)
    29     {
    30         printf("%05d %d ", list[i], nodes[list[i]]);
    31         if (i < list.size() - 1)
    32             printf("%05d
    ", list[i+1]);
    33         else
    34             printf("-1
    ");
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    1. 首次运行 Git 的配置
    本地git连接github
    Cygwin(linux)中vim配置
    ibatis入门
    XML(可扩展性标记语言)学习汇总一
    第一只小啊小爬虫(纪念下)
    Android SQLite DB的封装
    关于做项目的一些感想
    Git 初学三(git对象与git重置)
    Git 初学二(暂存区)
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11314363.html
Copyright © 2020-2023  润新知