当前位置:网站首页>02 linear structure 3 reversing linked list
02 linear structure 3 reversing linked list
2022-07-27 08:41:00 【Madness makes freedom】
02- Linear structure 3 Reversing Linked List
fraction 25
author Chen Yue
Company Zhejiang University
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
analysis :
For the former rev-1 Inverted linked list , The next address of the last node of each inverted linked list is the next inverted linked list
First address ;
about res Inverted linked list , If n%k==0, Then the next address of the last node is -1, Otherwise, the next node of the last node
The address is the first address that cannot form an inverted linked list .
/**
analysis :
For the former rev-1 Inverted linked list , The next address of the last node of each inverted linked list is the next inverted linked list
First address ;
about res Inverted linked list , If n%k==0, Then the next address of the last node is -1, Otherwise, the next node of the last node
The address is the first address that cannot form an inverted linked list .
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
struct Node
{
int addr,data,next;
int flag;
Node()
{
flag=2*maxn;
}
};
bool cmp(Node &a,Node &b)
{
return a.flag<b.flag;
}
int main()
{
int head,n,k;
scanf("%d%d%d",&head,&n,&k);
Node node[maxn];
// for(int i=0;i<maxn;++i)
// node[i].flag=0;
int addr;
for(int i=0;i<n;++i)
{
scanf("%d",&addr);
scanf("%d%d",&node[addr].data,&node[addr].next);
node[addr].addr=addr;
}
int i=0;
while(head!=-1)
{
node[head].flag=++i;
head=node[head].next;
}
n=i; // There are redundant nodes that are not on the linked list
sort(node,node+maxn,cmp);
/**
cout << "jidu\n";
for(int i=0;i<n;++i)
printf("%05d %d %05d\n",node[i].addr,node[i].data,node[i].next);
cout << "lo\n";
*/
int rev=n/k;
for(int i=0;i<rev;++i)
{
for(int j=k-1;j>=0;--j)
{
if(i!=rev-1) // If it is the last time of reversing the linked list
{
int index=i*k+j;
if(j==0) // If it is the last node of each trip
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[(i+2)*k-1].addr);
else
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[index-1].addr);
}
else
{
int index=i*k+j;
if(j==0)
{
if(n%k==0)
printf("%05d %d -1\n",node[index].addr,node[index].data);
else
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[(i+1)*k].addr);
}
else
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[index-1].addr);
}
}
}
for(int i=rev*k;i<n;++i) // The remaining nodes that cannot form an inverted linked list
{
if(i==n-1)
printf("%05d %d -1\n",node[i].addr,node[i].data);
else
printf("%05d %d %05d\n",node[i].addr,node[i].data,node[i+1].addr);
}
return 0;
}
边栏推荐
猜你喜欢

The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu

Node installation and debugging

redis 网络IO

说透缓存一致性与内存屏障

Alibaba cloud international receipt message introduction and configuration process

String type and bitmap of redis

Initial summary of flask framework creation project

第2章 前台数据展现

Process control - Branch

How to merge multiple columns in an excel table into one column
随机推荐
OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)
Explain cache consistency and memory barrier
Make a game by yourself with pyGame 01
网络IO总结文
CMD command and NPM command
3311. Longest arithmetic
OPPO 自研大规模知识图谱及其在数智工程中的应用
Have a good laugh
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
Functions and arrow functions
NIO示例
Alibaba cloud international receipt message introduction and configuration process
低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择
List delete collection elements
4276. 擅长C
说透缓存一致性与内存屏障
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
Connection failed during installation of ros2 [ip: 91.189.91.39 80]
Arm system call exception assembly
借生态力量,openGauss突破性能瓶颈