02-线性结构3 Reversing Linked List(PTA)

2023-11-09

02-线性结构3 Reversing Linked List(25 point(s))

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, andNext 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
Author: 陈越
Organization: 浙江大学
Time Limit: 400ms
Memory Limit: 64MB
Code Size Limit:16KB


初学数据结构, 十分惭愧, 这道题虐了我好几天;

但是最后一遍直接AC的感觉是十分美妙的;

现在对此题做些总结:

(一)单链表的倒转

本来一开始, 以我个人的性格, 总是喜欢自己研究, 结果搞了好几天都是各种段错误, 语法错误;

还误入歧途, 尝试了双向链表, 极其繁琐, 又不易调试;

而后终于, 借鉴https://www.2cto.com/kf/201601/485759.html博客的思想, 在链表倒转这一块算是有了较好的认识;

(二)采用递归

在第四个版本时, 重新架构代码;

重新思考题目, 发现无论如何, 都要遍历一次, 按顺序安放结点;

那么为何不把问题主要分解成:

1.根据地址找到结点;

2.将其放到正确的位置;


而显然, 要遍历到K == 1 或者 Address 找不到, 方可知道要采用倒转放置, 还是正序放置;

故递归做法, 显然是十分合适的;

当递归至最里层, 根据递归返回状态, 选择是要倒序, 还是正序;

而后层层回溯, 放置结点;


故总体时间复杂度为O(n);
n为结点地址为-1或找不到为止;
小于等于链表长度;
空间复杂度, 因有递归, 不是很懂, 当K较大时, 递归层数较深, 或许会较大;
但, 不考虑递归带来的影响, S(n);
n为总链表长度;


总结:

最后的时候, 一次性写完代码, 除了几个分号不小心为中文的, 其余没有错误, 一次性编译通过;

真的是十分的爽快;

在sublime上简单的测试了一下, 发现没有什么问题, 便上交PTA;

最后一次AC!

还是太弱, 小题目就花了好几天;

不过这道题下来, 对链表的使用确实有了很大的提升;


最后, 附代码:


/**********************/
/*    Title:
        Reversing_Linked_list_v0_4
/*    Author:
        cxy
/*    Start_Date:
        04242018
/*    Finished_Date:
        04242018
/*    FUNCTION:
        主要实现了,给定一系列的数据, 用单链表存储, 而后根据结点信息, 对应其内部排列关系, 进行给定位数的结点次序反转。
        一直反转到无法反转;
        输入:
        地址, 数据, 下一节点地址;
        输出:
        按行:地址, 数据, 下一节点地址;

        具体可见https://pintia.cn/problem-sets/951072707007700992/problems/972813177016877056
/*    Details:
        采用递归思想, 重新规划架构;
        避免前面版本的拖沓;
        无论如何, 都要遍历一次, 要重新排序;
        就是说, 在遍历同时, 直接考虑其位置;
        则共需遍历一次;
        故总体时间复杂度为O(n);
        n为结点地址为-1或找不到为止;
        小于等于链表长度;
        空间复杂度, 因有递归, 不是很懂, 当K较大时, 递归层数较深, 或许会较大;
        但, 不考虑递归带来的影响, S(n);
        n为总链表长度;
/**********************/

#include <stdio.h>
#include <stdlib.h>

#define ERROR_FIRST 1
#define ERROR         2
#define OK             3
#define WRONG        0

typedef int STATUS;
typedef struct LL_Node *p_llnode;
typedef struct Detail information;

struct Detail
{
    int Address;
    int Data;
    int Next;
};

struct LL_Node
{
    information node;
    p_llnode link;
};

p_llnode Read_List( int total_num );
p_llnode Reverse_List( p_llnode L, int start_address, int rev_num );
void Print_List( p_llnode L );
void Attach( p_llnode *rear, information INF );
STATUS Sub_Rev_List( p_llnode head, int *Address, int rev_num, p_llnode sub_list_head, p_llnode *sub_list_rear );
p_llnode Get_Needed_Node( p_llnode head, int Address );
void Get_Sub_FIRST_Head( p_llnode p_needed_node, p_llnode sub_list_head, p_llnode *sub_list_rear );
void Head_Insert( p_llnode p_needed_node, p_llnode sub_list_head );
void Rear_Insert( p_llnode p_needed_node, p_llnode *sub_list_rear );
void FREE_Left_Node( p_llnode head );
void Reindex_LLnode( p_llnode L );

int main(int argc, char const *argv[])
{
    p_llnode L = NULL;
    int start_address = 0, total_num = 0, rev_num = 0;

    scanf("%d %d %d", &start_address, &total_num, &rev_num);

    L = Read_List( total_num );
    L = Reverse_List( L, start_address, rev_num );

    Reindex_LLnode( L );
    Print_List( L );

    return 0;
}

p_llnode Read_List( int total_num )
{
    information INF;
    p_llnode head, rear, temp = NULL;

    if ( total_num <= 0)
        return NULL;

    rear = head = (p_llnode)malloc(sizeof(struct LL_Node));

    while ( total_num-- )
    {
        scanf("%d %d %d", &INF.Address, &INF.Data, &INF.Next);
        Attach( &rear, INF );
    }
    rear->link = NULL;

    temp = head;
    head = head->link;
    free(temp);

// test_read
    // temp = head;
    // while ( head )
    // {
    //     printf("%05d %d %05d\n", head->node.Address, head->node.Data, head->node.Next);
    //     head = head->link;
    // }

    return head;
}

// recursion;
p_llnode Reverse_List( p_llnode L, int start_address, int rev_num )
{
    p_llnode head, new_list_head, new_list_rear, sub_list_head, sub_list_rear, temp;
    int Address = start_address;
    int IS_FIRST_SUB_REV = 1;

    new_list_head = new_list_rear = sub_list_head = sub_list_rear = NULL;

    head = (p_llnode)malloc(sizeof(struct LL_Node));
    head->link = L;

    sub_list_head = (p_llnode)malloc(sizeof(struct LL_Node));
    sub_list_head->link = NULL;

    while ( Sub_Rev_List( head, &start_address, rev_num, sub_list_head, &sub_list_rear ) == OK )
    {
        if ( IS_FIRST_SUB_REV )
        {
            IS_FIRST_SUB_REV = 0;
            new_list_head = sub_list_head->link;        // new_list_head不带头空结点;
        }
        else
            new_list_rear->link = sub_list_head->link;
            
        new_list_rear = sub_list_rear;

        sub_list_head->link = NULL;
    }

    new_list_rear->link = sub_list_head->link;
    new_list_rear = sub_list_rear;
    // new_list_rear->link = NULL;
    FREE_Left_Node( head );
    free( head );
    free( sub_list_head );

    return new_list_head;
}

void Print_List( p_llnode L )
{
    while ( L->node.Next != -1 )
    {
        printf("%05d %d %05d\n", L->node.Address, L->node.Data, L->node.Next);
        L = L->link;
    }
    printf("%05d %d %d\n", L->node.Address, L->node.Data, L->node.Next);
}

void Attach( p_llnode *rear, information INF )
{
    p_llnode temp;

    temp = (p_llnode)malloc(sizeof(struct LL_Node));

    // temp->node = *INF? 结构体直接赋值, 可能会有问题; 虽然test时是OK的, 此点保留意见;
    temp->node.Address = INF.Address;
    temp->node.Data = INF.Data;
    temp->node.Next = INF.Next;
    temp->link = NULL;

    (*rear)->link = temp;
    *rear = temp;
}

STATUS Sub_Rev_List( p_llnode head, int *Address, int rev_num, p_llnode sub_list_head, p_llnode *sub_list_rear )
{
    p_llnode temp, p_needed_node;
    STATUS rev_status;

    p_needed_node = Get_Needed_Node( head, *Address );
    if ( !p_needed_node )        // Sub_Rev非正常终止信号; 此时表示所给Address无法指定结点, 也可能为-1;
        return ERROR_FIRST;

    *Address = p_needed_node->node.Next;        // 更新Address;

    if ( rev_num == 1 )    // Sub_Rev正常终止信号;
    {
        Get_Sub_FIRST_Head( p_needed_node, sub_list_head, sub_list_rear );
        return OK;
    }

    --rev_num;

    rev_status = Sub_Rev_List( head, Address, rev_num, sub_list_head, sub_list_rear );
    switch ( rev_status )
    {
        case ERROR_FIRST:
            Get_Sub_FIRST_Head( p_needed_node, sub_list_head, sub_list_rear );
            return ERROR;

        case ERROR:
            Head_Insert( p_needed_node, sub_list_head );    // 注意! sub_list_head是p_llnode型;
            return ERROR;

        case OK:
            Rear_Insert( p_needed_node, sub_list_rear );    // 注意! sub_list_rear是p_llnode *型;
            return OK;

        default:
            return WRONG;
    }
}

p_llnode Get_Needed_Node( p_llnode head, int Address )
{
    p_llnode p_front_node, temp;

    p_front_node = head;

    while ( p_front_node->link && p_front_node->link->node.Address != Address )
        p_front_node = p_front_node->link;

    if ( !p_front_node->link )
        return NULL;

    temp = p_front_node->link;
    p_front_node->link = temp->link;
    temp->link  = NULL;

    return temp;
}

void Get_Sub_FIRST_Head( p_llnode p_needed_node, p_llnode sub_list_head, p_llnode *sub_list_rear )
{
    *sub_list_rear = sub_list_head->link = p_needed_node;
}

void Head_Insert( p_llnode p_needed_node, p_llnode sub_list_head )
{
    p_needed_node->link = sub_list_head->link;
    sub_list_head->link = p_needed_node;
}

void Rear_Insert( p_llnode p_needed_node, p_llnode *sub_list_rear )
{
    (*sub_list_rear)->link = p_needed_node;
    *sub_list_rear = p_needed_node;
}

void FREE_Left_Node( p_llnode head )
{
    p_llnode temp;

    while ( head->link )
    {
        temp = head->link;
        head->link = temp->link;
        free(temp);
    }
}

void Reindex_LLnode( p_llnode L )
{
    while ( L->link )
    {
        L->node.Next = L->link->node.Address;
        L = L->link;
    }
    L->node.Next = -1;
}



                                                                                                            04252018-22:06---第一次

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

02-线性结构3 Reversing Linked List(PTA) 的相关文章

  • VMware Workstation 无法连接到虚拟机。请确保您有权运行该程序、访问该程序使用的所有目录以及访问所有临时文件目录的解决方法

    VMware Workstation 无法连接到虚拟机 请确保您有权运行该程序 访问该程序使用的所有目录以及访问所有临时文件目录 这个问题刚刚用虚拟机的人可能会经常遇到 解决方法就是 在开始中搜索服务 点击服务正在本电脑运行 注意 这里演示
  • CloudCompare 二次开发(5)——非插件中的PCL环境配置(均匀采样为例)

    目录 一 概述 二 CMakeLists txt 三 源码编译 四 代码示例 五 结果展示 本文由CSDN点云侠原创 原文链接 爬虫网站自重 一 概述 在进行CloudCompare二次开发的时候 可以直接在CloudCompare的核心功
  • 推动政府数字化转型进入新阶段

    推动政府数字化转型进入新阶段 公司近两年比较关注数字化转型和金融科技 打算今年重点了解一下 在网上看到了一个文章 感觉还不错 转载到这里 本文转自人民政协网上的 推动政府数字化转型进入新阶段 1 国家政策 国务院近日发布的 十四五 数字经济
  • 智慧城市智慧零售受益于5G和AI双核驱动

    支付宝推出了刷脸支付 我们只需要对准摄像头让它把我们脸部的特征完全识别出来 然后就可以进行支付了 那么这种人脸支付会用在很多地方 很简单 我们去超市购物的时候 以往你要么用卡要么给现金 或者你掏出手机来支付 但是怎么也得输入密码或者按指纹
  • MySQL自增主键详解

    一 自增值保存在哪儿 不同的引擎对于自增值的保存策略不同 1 MyISAM引擎的自增值保存在数据文件中 2 InnoDB引擎的自增值 在MySQL5 7及之前的版本 自增值保存在内存里 并没有持久化 每次重启后 第一次打开表的时候 都会去找
  • chrome浏览器:您的连接不是私密连接,burp抓包

    问题 您的连接不是私密连接 处理 简简单单 跟着我来没错 不要浪费时间再找了 插件设置 SwitchyOmega 开启代理访问http burp CA下载证书 chrome flags Allow invalid certificates
  • 第3章 数据库结构设计

    3 1数据库概念设计 数据库概念设计主要解决数据需求 即如何准确地理解数据需求 真实地把应用领域中要处理的数据组织 定义描述清楚 以支持数据库设计后续阶段的工作 3 1 1概念设计的任务 数据库概念设计阶段的目标是 1 定义和描述应用领域涉
  • 2024王道数据结构P17No11

    一个长度为L L gt 1 的升序序列S 处在第L 2位置 向下取整 的数称为S的中位数 例如 序列S1 11 13 15 17 19 则中位数为15 两个序列的中位数是含他们所有元素的升序序列的中位数 例如 S2 2 4 6 8 20 则
  • 【毕业设计】深度学习身份证识别系统 - 机器视觉 python

    文章目录 0 前言 1 实现方法 1 1 原理 1 1 1 字符定位 1 1 2 字符识别 1 1 3 深度学习算法介绍 1 1 4 模型选择 2 算法流程 3 部分关键代码 4 效果展示 5 最后 0 前言 Hi 大家好 这里是丹成学长的
  • 学习总结7.1 Linux Rsh服务器

    在线安装是指不需要用户亲自下对应软件的包 但是需要对应系统能够访问互联网 不同的Linux系统使用不同的工具进行在线安装软件 常见的在线安装软件的工具如下所示 Ubuntu Debian系统使用apt get进行在线安装软件 Redhat
  • 动态粒子爱心,表白神器源码

    效果 https www douyin com user self modal id 7187722820967763237 源码 from tkinter import from matplotlib import pyplot as p
  • FCN的代码解读

    目录 模型初始化 VGG初始化 FCN初始化 图片的预处理 图片处理 图片编码 计算相关参数 模型训练 一个小问题 完整代码 参考 最近浅研究了一下关于图像领域的图像分割的相关知识 发现水还是挺深的 因为FCN差不多也是领域的开山鼻祖 所以

随机推荐

  • Android无线网络调试手机

    adb tcpip 5555 adb下载地址 http download clockworkmod com test UniversalAdbDriverSetup msi 3 在设备中下载超级终端 是andriod软件 设置端口 su s
  • JVM笔记-黑马-1

    文章目录 视频资源地址 笔记资源地址 我的笔记 1 什么是JVM 2 学习jvm的作用 3 常见的jvm 4 学习路线 5 内存结构 程序计数器 作用 6 内存结构 程序计数器 特点 7 内存结构 虚拟机栈 8 内存结构 虚拟机栈的演示 9
  • C++文件操作和文件流

    C 文件操作和文件流 1文件的概念 2 文件流的分类 2 打开文件 2 1 通过类对象调用 open 函数打开一个文件 2 2 通过类对象构造函数打开文件 3 关闭文件 4 读写文件 4 1 文本文件的读写 4 2 二进制文件的读写 1文件
  • ESP8266之AT指令

    一 8266作为client 1 AT 功能 测试8266能否工作 2 AT CWMODE 3 功能 设置工作模式 1 station模式 2 ap模式 3 ap station复位保存当前值 3 AT RST 功能 复位 4 AT CWL
  • Android利用AIDL实现apk之间跨进程通信

    AIDL 最广泛与最简单的应用是与四大组件之一 Serivce 的配合使用了 我们都知道 启动一个 Serivce 有两种方式 1 通过 startService 的方式 2 通过 bindService 的方式 通过 binService
  • 图像处理之目标检测入门总结

    点击上方 小白学视觉 选择加 星标 或 置顶 重磅干货 第一时间送达 本文转自 机器学习算法那些事 本文首先介绍目标检测的任务 然后介绍主流的目标检测算法或框架 重点为Faster R CNN SSD YOLO三个检测框架 本文内容主要整理
  • linux安装自动化部署工具jenkins

    创建工程目录 mkdir home software jenkins 创建工作空间 mkdir home workspaces jenkins 进入工程目录 cd home software jenkins 下载Jenkins rpm安装包
  • 伪代码及其实例讲解

    伪代码 Pseudocode 是一种算法描述语言 使用伪代码的目的是为了使被描述的算法可以容易地以任何一种编程语言 Pascal C Java etc 实现 因此 伪代码必须结构清晰 代码简单 可读性好 并且类似自然语言 介于自然语言与编程
  • 基于svg.js实现对图形的拖拽、选择和编辑操作

    本文主要记录如何使用 svg js 实现对图形的拖拽 选择 图像渲染及各类形状的绘制操作 1 关于SVG SVG 是可缩放的矢量图形 使用XML格式定义图像 可以生成对应的DOM节点 便于对单个图形进行交互操作 比CANVAS更加灵活一点
  • 分享一下Python数据分析常用的8款工具

    Python是数据处理常用工具 可以处理数量级从几K至几T不等的数据 具有较高的开发效率和可维护性 还具有较强的通用性和跨平台性 Python可用于数据分析 但其单纯依赖Python本身自带的库进行数据分析还是具有一定的局限性的 需要安装第
  • 移动端开发技术小结(前端)

    移动端开发技术小结 前端 移动端处理webkit内核即可 浏览器的私有前缀只需要考虑添加 webkit 布局视口 视觉视口 理想视口 将布局宽度改为视觉视口 图片 2倍图 3倍图 背景缩放 background size 背景图片宽度 背景
  • windows 系统安装sonarqube

    SonarQube是一种自动代码审查工具 用于检测代码中的错误 漏洞和代码异味 它可以与您现有的工作流程集成 以便在项目分支和拉取请求之间进行连续的代码检查 官方网站 https www sonarqube org 1 使用前提条件 运行S
  • FTP-读取指定目录下的文件,上传到FTP服务器,一键复制黏贴,就是这么丝滑~

    背景 需要定时将服务器下的日志文件上传到指定FTP服务器的目录下 并通知第三方平台文件已上传 FTP服务器模拟工具 application yml配置 spring logfilepath home jboss server default
  • Cesium Terrain Builder (CTB) 简单使用_地形切片

    Cesium Terrain Builder CTB 简单使用 地形切片 目录 Cesium Terrain Builder CTB 简单使用 地形切片 官网地址 win r cmd 打开命令提示符工具运行 Create a GDAL Vi
  • windows计算机无法打开,为什么我的电脑无法运行Win11?原因可能是这个

    原标题 为什么我的电脑无法运行Win11 原因可能是这个 为什么我的电脑无法运行Win11 原因可能是这个 微软已经在日前正式发布Windows 11操作系统 虽然新系统的更新升级与发布并非同步进行 甚至现在连官方都未公开预览版 但由于此前
  • eccms静态页面出现出现基础链接已关闭,无法链接到远程服务器错误的解决办法

    出现 基础链接已关闭 无法链接到远程服务器 错误 一 系统组件错误 如果属于系统Socket组件错误 可以重启socket组件 netsh winsock reset 进行解决 二 实际发生的原因 由于实际情况所需 禁止服务器访问外网 解决
  • 【C++笔记】《C++编程思想-卷一》笔记

    C 编程思想 笔记 Volume 1 第一章 对象导言 OOP ObjectOriented Programming 面对对象编程 UML Unified Model Language 统一建模语言 堆 stack 和栈 heap 预备知识
  • dgl 操作

    dgl图的基本操作 dgl简单使用 udf函数怎么写 通过edges 打得到两端的列表 将所有nodes换成edges 截错图了 通过nodes则不是很好使 返回很多的tensor原因是这个函数运行好几遍
  • Seq2Seq模型学习(pytorch)

    在看pytorch的官方英文例子 做些笔记 如有纰漏请指正 原文 https pytorch org tutorials beginner chatbot tutorial html 数据准备 首先是单词编码 seq2seq的单词编码的方式
  • 02-线性结构3 Reversing Linked List(PTA)

    02 线性结构3 Reversing Linked List 25 point s Given a constant K and a singly linked list L you are supposed to reverse the