当前位置:网站首页>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;
}
边栏推荐
- while Loop
- regular expression
- JS advanced knowledge - function
- Set set
- I drew a Gu ailing with characters!
- Software test interview questions (key points)
- 4278. Summit
- Solution of database migration error
- Hundreds of people participated. What are these people talking about in the opengauss open source community?
- 低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择
猜你喜欢

User management - restrictions

Day3 -- flag state holding, exception handling and request hook

开怀一笑

Create a project to realize login and registration, generate JWT, and send verification code

openGauss之TryMe初体验

How to view instances of software objects in QSIM?

One book 1201 Fibonacci sequence

低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择

How to permanently set source

海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
随机推荐
General view, DRF view review
Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum
All in one 1353 -- expression bracket matching (stack)
JS advanced knowledge - function
JS basic exercises
Query and association of flask to database
All in one 1251 - Fairy Island for medicine (breadth first search)
Is online account opening safe? Want to know how securities companies get preferential accounts?
VS Code中#include报错(新建的头文件)
03.使用引号来监听对象嵌套值的变化
List删除集合元素
Create a project to realize login and registration, generate JWT, and send verification code
众昂矿业:新能源行业快速发展,氟化工产品势头强劲
NIO总结文——一篇读懂NIO整个流程
Cache consistency and memory barrier
百人参与,openGauss开源社区这群人都在讨论什么?
Installation and use of Supervisor
杭州电子商务研究院发布“数字化存在”新名词解释
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
How to view instances of software objects in QSIM?