当前位置:网站首页>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;
}
边栏推荐
- 1178 questions of Olympiad in informatics -- ranking of grades
- 4276. 擅长C
- Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
- How to upload qiniu cloud
- 4274. Suffix expression
- Creation and simple application of QPushButton button button
- Solution to the program design of the sequence structure of one book (Chapter 1)
- 4279. Cartesian tree
- OPPO 自研大规模知识图谱及其在数智工程中的应用
- Iterators and generators
猜你喜欢

Solution to the program design of the sequence structure of one book (Chapter 1)

Chapter 2 foreground data display

How to permanently set source

Eval and assert execute one sentence Trojan horse

微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?

regular expression

借生态力量,openGauss突破性能瓶颈

众昂矿业:新能源行业快速发展,氟化工产品势头强劲

4274. Suffix expression

Realization of backstage brand management function
随机推荐
Functions and arrow functions
Function realization of order system
Openresty + keepalived to achieve load balancing + IPv6 verification
Explain cache consistency and memory barrier
4275. Dijkstra sequence
How to upload qiniu cloud
3428. 放苹果
All in one 1353 -- expression bracket matching (stack)
Day5 - Flame restful request response and Sqlalchemy Foundation
Vertical align cannot align the picture and text vertically
“寻源到结算“与“采购到付款“两者有什么不同或相似之处?
说透缓存一致性与内存屏障
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
Arm system call exception assembly
众昂矿业:新能源行业快速发展,氟化工产品势头强劲
Introduction to depth first search (DFS)
Do a reptile project by yourself
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
STM32 small bug summary
JS检测客户端软件是否安装