当前位置:网站首页>Pat class B 1025 reverse linked list
Pat class B 1025 reverse linked list
2022-06-23 05:58:00 【Octopus bro】
1025. Reverse a linked list (25)
Given a constant K And a single linked list L, Please write a program to L Every time K Node inversion . for example : Given L by 1→2→3→4→5→6,K by 3, Then the output should be 3→2→1→6→5→4; If K by 4, Then the output should be 4→3→2→1→5→6, That is, less than K Elements do not invert .
Input format :
Each input contains 1 Test cases . Each test case is No 1 Line gives 1 The address of the node 、 The total number of nodes is a positive integer N(<= 105)、 And positive integers K(<=N), That is, the number of child chain nodes requiring inversion . The address of the node is 5 Bit nonnegative integer ,NULL Address use -1 Express .
Next there is N That's ok , The format of each line is :
Address Data Next
among Address Is the node address ,Data Is the integer data saved in this node ,Next Is the address of the next node .
Output format :
For each test case , Output the inverted linked list in sequence , Each node on it occupies a row , The format is the same as the input .
sample input :00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218sample output :
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
Ideas : The data element of this operation is a containing address 、 data 、 The structure of the next node address , Apply for one first 100001 Array of structures of , Using dynamic memory allocation , Directly declare the array in dev Overflow in . Take the array subscript as the address , Each time you input a node, you will find the subscript position corresponding to the address and save it . After storing all nodes , In the order of the linked list ( That is, start from the address of the first node , Until I met -1 end ) After sorting, it is stored in another array , Finally, in the ordered array K In reverse order . Be careful , Answer: the Lord got the card at the last test point long long time!! Finally, it is found that the test case contains nodes that are not in the linked list ~,~
So when you meet next by -1 Exit input when .
One 、 Starting variable
1.input[100001], The node used to save the input
2.sortQ[100001], It is used to save the sequence arranged according to the linked list sequence
3.add The address of the first node in the linked list
4.N Number of nodes
5.K Number of subsequences
Two 、 Algorithm
1. Enter all nodes
2. take input The nodes in the are placed in the order of addresses sortQ In sequence ;
3. Invert the nodes in the subsequence
4. Modify the of each node after the last reverse order next And the output
3、 ... and 、 Code
#include "stdio.h"
#include "stdlib.h"
typedef struct{
int address;// Address
int data;// data
int next;// Next node address
}node;
int main()
{
node * input = (node *)malloc(sizeof(node) * 100001);// The node used to store the input
node * sortQ = (node *)malloc(sizeof(node) * 100001);// Used to store ordered nodes
int add,N,K;
scanf("%d %d %d",&add, &N, &K);// Input starting variable
//1. Input node
for(int i = 0; i < N; i++)
{
node tmp;
scanf("%d %d %d",&tmp.address, &tmp.data, &tmp.next);
input[tmp.address] = tmp;
}
//2. take input The nodes in the are placed in the order of addresses sortQ In sequence ;
for(int i = 0; i < N; i++)
{
sortQ[i] = input[add];
add = input[add].next;
// The last test point is to add this if Later through , That is, change as soon as the end is met N Out of the loop
// If it is not added, you will access the node that has not stored data , There will be errors in the reverse order
if(add == -1)
{
N = i + 1;
break;
}
}
int converseNum = N / K;// The number of subsequences to reverse
//3. Invert the nodes in the subsequence
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];// The first i( from 0 Start ) The... In the subsequence j And the penultimate j Exchange
sortQ[i * K + j] = sortQ[i * K + K - 1 - j];
sortQ[i * K + K - 1 - j] = tmp;
}
}
//4. here sortQ The nodes in are already inverted sequences , Just modify the of each node next
int i = 0;
for(i = 0; i < N - 1; i++)// Before modifying and exporting n-1 Nodes
{
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;// Of the last node next by -1
printf("%05d %d %d\n",sortQ[i].address,sortQ[i].data,sortQ[i].next);
return 0;
}
边栏推荐
- JS面试题----防抖函数
- Genetic engineering of AI art? Use # artbreeder to change any shape of the image
- Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
- PAT 乙等 1009 C语言
- True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
- PAT 乙等 1021 个位数统计
- 使用链表实现两个多项式相加和相乘
- Activity启动模式和生命周期实测结果
- Excel sheet column number for leetcode topic resolution
- Pat class B 1019 C language
猜你喜欢

How to specify the output path of pig register Project Log

【Cocos2d-x】截图分享功能

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

jvm-06. Garbage collector

jvm-04.对象的内存布局

MDM data cleaning function development description

【Cocos2d-x】可擦除的Layer:ErasableLayer
![[Stanford Jiwang cs144 project] lab2: tcpreceiver](/img/70/ceeca89e144907226f29575def0e4d.png)
[Stanford Jiwang cs144 project] lab2: tcpreceiver

The construction of digital factory can be divided into three aspects

True question of MySQL interview (29) -- case - finding favorite movies
随机推荐
Visdom draws multiple dynamic loss curves
Pit filling for abandoned openssl-1.0.2 (.A to.So)
Digital collections - new investment opportunities
Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference
jvm-01. Instruction rearrangement
Pat class B 1009 C language
jvm-06. Garbage collector
node中操作mongoDB
Pat class B 1018 C language
Ansible 使用普通用户管理被控端
Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration
[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project
JS interview question - anti shake function
Basic calculator II for leetcode topic analysis
PAT 乙等 1015 C语言
The hierarchyviewer tool cannot find the hierarchyviewer location
MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
PAT 乙等 1016 C语言
ORB_SLAM2运行
PAT 乙等 1023 组个最小数