• 1074 Reversing Linked List (25 分)


    1074 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 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 (105​​) which is the total number of nodes, and a positive K (N) 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

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<set>
    #include<stack>
    #include<string.h>
    #include<cstdio>
    #include <unordered_map>
    #include<cmath>
    
    using namespace std;
    struct Node
    {
        int address,data,next;
    };
    
    int main()
    {
        int head,n,k;
        scanf("%d%d%d",&head,&n,&k);
        map<int,Node> mp;
        //Node temp;
        int a,b,c;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            mp[a]={a,b,c};
        }
        vector<Node>node;
        for(int i=head;i!=-1;i=mp[i].next)
            node.push_back(mp[i]);
        int len=node.size();
        for(int i=0;i<len;i+=k)
        {
            if(len-i<k)
                break;
            int temp=1;
            for(int j=i;j<(i+i+k)/2;j++)
            {
                swap(node[j],node[i+k-temp]);
                temp++;
            }
    
        }
        for(int i=0;i<len-1;i++)
        {
            printf("%05d %d %05d
    ",node[i].address,node[i].data,node[i+1].address);
        }
        printf("%05d %d -1
    ",node[len-1].address,node[len-1].data);
        return 0;
    }
  • 相关阅读:
    C++ 解析CSV文件
    MFC/WTL 设置背景图和控件透明的方法
    VS2008安装x64版本所遇问题
    VS2012 安装番茄插件
    16年面试提问
    git 使用笔记
    03_运算符、键盘录入、流程控制
    02_java关键字、表识符、注释、进制转换、补码反码、数据类型转换
    01_计算机和java基础
    10 js一维数组、一维数组细节
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327740.html
Copyright © 2020-2023  润新知