当前位置:网站首页>PAT 乙等 1025 反转链表
PAT 乙等 1025 反转链表
2022-06-23 04:11:00 【章鱼bro】
1025. 反转链表 (25)
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。
输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。
输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入样例:00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218输出样例:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
思路:本题操作的数据元素是一个包含地址、数据、下一个节点地址的结构体,先申请一个100001的结构体的数组,采用动态分配内存,直接声明数组在dev中会溢出。将数组下标作为地址,每输入一个节点就找到地址对应的下标位置存进去。在存储好所有结点以后,按照链表的顺序(即从第一个节点的地址开始寻找,直到遇到-1结束)排序之后存入另一个数组,最后在排好序的数组中将K个元素倒序。 注意,答主在最后一个测试点卡了long long time!! 最后发现该测试用例是含有不在链表中的节点~,~
所以要在遇到next为-1时就退出输入。
一、起始变量
1.input[100001],用来保存输入的节点
2.sortQ[100001],用来保存按照链表序列排好的序列
3.add链表中第一个节点的地址
4.N节点个数
5.K子序列个数
二、算法
1.输入所有节点
2.将input中的节点按照地址排好序放入sortQ序列中;
3.将子序列里的节点进行反转
4.修改最后逆序后的每个节点的next并输出
三、代码
#include "stdio.h"
#include "stdlib.h"
typedef struct{
int address;//地址
int data;//数据
int next;//下一个节点地址
}node;
int main()
{
node * input = (node *)malloc(sizeof(node) * 100001);//用来存储输入的节点
node * sortQ = (node *)malloc(sizeof(node) * 100001);//用来存储排好序的节点
int add,N,K;
scanf("%d %d %d",&add, &N, &K);//输入起始变量
//1.输入节点
for(int i = 0; i < N; i++)
{
node tmp;
scanf("%d %d %d",&tmp.address, &tmp.data, &tmp.next);
input[tmp.address] = tmp;
}
//2.将input中的节点按照地址排好序放入sortQ序列中;
for(int i = 0; i < N; i++)
{
sortQ[i] = input[add];
add = input[add].next;
//最后一个测试点在加上这个if以后通过,即只要遇到结尾即更改N跳出循环
//若不加则会访问到没有存入数据的节点,在之后的倒序就会出现错误
if(add == -1)
{
N = i + 1;
break;
}
}
int converseNum = N / K;//要反转的子序列数目
//3.将子序列里的节点进行反转
for(int i = 0; i < converseNum; i++)
{
for(int j = 0; j < K / 2; j++)
{
node tmp = {0, 0, 0};
tmp = sortQ[i * K + j];//第i(从0开始)个子序列中的第j个与倒数第j个交换
sortQ[i * K + j] = sortQ[i * K + K - 1 - j];
sortQ[i * K + K - 1 - j] = tmp;
}
}
//4.此时sortQ中的节点已经是反转后的序列,只需修改每个节点的next
int i = 0;
for(i = 0; i < N - 1; i++)//修改并输出前n-1个节点
{
sortQ[i].next = sortQ[i + 1].address;
printf("%05d %d %05d\n",sortQ[i].address,sortQ[i].data,sortQ[i].next);
}
sortQ[i].next = -1;//最后一个节点的next为-1
printf("%05d %d %d\n",sortQ[i].address,sortQ[i].data,sortQ[i].next);
return 0;
}
边栏推荐
- Visdom draws multiple dynamic loss curves
- About replay attack and defense
- Jsonfield annotation in fastjson
- FS4059A与FS5080E充电芯片的区别
- 制造业数字化转型存在问题及原因分析
- 啊哈C语言 第8章 游戏时间到了(第29讲)
- Xa mode explanation and code implementation of four Seata modes
- Heimdall Database Proxy横向扩展提高20倍
- Wechat applet: a new interesting test
- [opencv450] image subtraction, binarization, threshold segmentation
猜你喜欢

Win11如何开启移动热点?Win11开启移动热点的方法

技能自检 | 想当测试Leader,这6项技能你会吗?

Visdom draws multiple dynamic loss curves

FS2119A同步升压IC输出3.3V和FS2119B同步升压IC输出5V

Management system of borrowed books based on SSM framework

Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter

数字化工厂建设可划分为三个方面

MDM data cleaning function development description

Qimen dunjia assistant decision software

Wechat applet: elderly blessing short video
随机推荐
Is there a real part-time job online? How do college students find part-time jobs in summer?
数字化工厂建设可划分为三个方面
Markdown add background color to the picture
51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices
英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件
Shutdown shutdown command
移动电源快充QC3.0方案芯片IP5318快充方案
Jenkins installs and deploys and automatically builds and publishes jar applications
ssm项目搭建
Today's sleep quality record 80 points
ORB_SLAM2运行
[opencv450] image subtraction, binarization, threshold segmentation
Management system of borrowed books based on SSM framework
C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
GDB data reading in GDAL (III) of GIS
Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
树莓派assert初步使用练习
MySQL面试真题(二十九)——案例-找到爱看的电影
Composite API