• 02-线性结构3 Reversing Linked List


    02-线性结构3 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Sample Output:

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

    代码长度限制
    16 KB
    时间限制
    400 ms
    内存限制
    64 MB
    C++ (g++)

    思路:

    用Data数组存数值,用Next数组存下一节点的地址,两个都以当前节点的地址为下标,同时记录了值,地址值,下一节点的地址。
    用OriAdr数组用来存输入数据的地址值
    开一个vector容器用来存反转后节点的地址,然后根据这个地址下标,输出地址、值、下一节点的地址。
    Read(int *Data,int * Next, int N) 读入数据
    Print_Format (int num) 格式化输出五位数地址

    Reverse(int * Data,int * Next,int first,int K)
    逆置链表
    首先记录节点的个数
    然后根据个数确定够不够K,逆不逆置
    如果不能逆置的话,直接将剩下的节点地址值存入Ans中
    如果能逆置的话,将这一组节点的地址值倒序压入Ans中
    如果遍历完整个链表节点的话,退出循环。
    输出逆置后的链表
    注意结尾不能有多余空格
    主函数就很简单。

    AC代码:

    #include
    using namespace std;
    #define maxsize 100005
    int Data[maxsize];
    int Next[maxsize];
    int OriAdr[maxsize];//输入顺序的地址
    vector<int> Ans;//反转后节点的地址
    void Read(int *Data,int *Next, int N)
    {
        for(int i=0;i<N;i++)
        {
            int address,data,next;
            cin>>address>>data>>next;
            Data[address]=data;//以地址为下标
            Next[address]=next;
        }
    }
    
    void Print_Format(int num)
    {
        //格式化输出五位地址数
        if(num==-1) {cout<<-1;return;}
        int d=10000;
        for(int i=0;i<5;i++)
        {
            int temp=num/d;
            while(temp/10!=0) temp=temp%10;
            cout<<temp;
            d/=10;
        }
    }
    void Reverse(int* Data,int* Next,int first,int K)
    {
        int temp=first;
        int cnt=0;
        //计算有效节点,题目所给有些节点是多余的
        while(temp!=-1)
        {
            cnt++;
            OriAdr[cnt]=temp;
            temp =Next[temp];//按照链表地址顺序索引
        }
        for(int i=0;;i+=K)
        {
            //若剩余节点不足k,则将余下节点按照顺序加入Ans中
            if(i+K>cnt)
            {
                i++;//从第i+1个节点开始
                while(i<=cnt)
                {
                    Ans.push_back(OriAdr[i]);
                    i++;
                }
                break;
            }
            int j=i+K;
            while(j>i)
            {
                Ans.push_back(OriAdr[j]);
                j--;
            }
            if(i+K==cnt) break;
        }
        for(auto it=Ans.begin();it!=Ans.end();it++)
        {
            Print_Format(*it);
            cout<<" "<<Data[*it]<<" ";
            if(it+1==Ans.end()) cout<<-1;
            else
            {
                Print_Format(*(it+1)); cout<<endl;
            }
        }
    }
    int main()
    {
        int first,N,K;//首地址,节点个数,反转个数
        cin>>first>>N>>K;
        Read(Data,Next,N);
        Reverse(Data,Next,first,K);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
  • 相关阅读:
    JavaWeb购物系统(十二)搜索联想词的添加
    多通道反向字典模型
    数组——二分查找
    二硫化钼量子点掺杂的ZnO纳米粒子(MoS2@ZnO)|负载氟西汀MoS2二硫化钼纳米片|超顺磁性碳化钽(Ta4C3-IONP-SPs)纳米复合材料
    成本高、落地难、见效慢,开源安全怎么办?
    UML测试题(用例图基础b)
    CentOS 7上创建Python 3虚拟环境
    MySQL数据库优化的几种方式(笔面试必问)
    约束的概念和分类(包含外键约束)
    用免费GPU部署自己的stable-diffusion-学习笔记
  • 原文地址:https://blog.csdn.net/manerzi/article/details/127728416