1074. Reversing Linked List (25)

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
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100100;
struct Node{int address,data,next;int order;
}node[maxn];bool cmp(Node a,Node b){return a.order < b.order;
}
int main(){int i,address;for(i = 0; i < maxn; i++){node[i].order = maxn;}int begin,n,k;  //起始节点地址,节点数目,分组数 scanf("%d%d%d",&begin,&n,&k);for(i = 0; i < n; i++){scanf("%d",&address);scanf("%d%d",&node[address].data,&node[address].next);node[address].address = address;}int p = begin,count = 0;while(p != -1){node[p].order = count++;p = node[p].next;}sort(node,node+maxn,cmp);n = count; //因为count=0占一个有效节点,退出循环时,count值就是有效节点 for(i = 0; i < n/k; i++){ //枚举完整的n/k块 for(int j = (i+1)*k - 1; j > i*k; j-- ){ //每块的第i个倒着输出,剩余最后一个节点 printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address);}printf("%05d %d ",node[i*k].address,node[i*k].data); //每块的最后一个节点的前两项数据 if(i < n/k -1)  { //如果是非最后一块节点 printf("%05d\n",node[(i+2)*k-1].address);}else{   //如果是最后一块节点 if(n % k == 0) printf("-1\n");  //刚好除整 else{   //如果最后一个节点不规则 printf("%05d\n",node[(i+1)*k].address);for(i = n/k*k; i < n; i++){printf("%05d %d ",node[i].address,node[i].data);if(i < n - 1){printf("%05d\n",node[i+1].address);}else{printf("-1\n");}// else} // for(i)}//else}//else} //for(i)return 0;
}

 

转载于:https://www.cnblogs.com/wanghao-boke/p/8550233.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385417.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Leetcode | 47】 222. 完全二叉树的节点个数

给出一个完全二叉树&#xff0c;求出该树的节点个数。 说明&#xff1a; 完全二叉树的定义如下&#xff1a;在完全二叉树中&#xff0c;除了最底层节点可能没填满外&#xff0c;其余每层节点数都达到最大值&#xff0c;并且最下面一层的节点都集中在该层最左边的若干位置。若最…

makefile中的两个函数(wildcard和patsubst)

(1) wildcard函数 作用是查找指定目录下指定类型的文件&#xff0c;并最终返回一个环境变量&#xff0c;需要用$取值赋值给另一个环境变量&#xff01;该函数只有一个参数&#xff0c;如取出当前目录下的所有.c文件&#xff0c;并赋值给allc普通变量&#xff1a; allc$(wildc…

231. 2的幂

给定一个整数&#xff0c;编写一个函数来判断它是否是 2 的幂次方。 示例 1: 输入: 1 输出: true 解释: 20 1 示例 2: 输入: 16 输出: true 解释: 24 16 示例 3: 输入: 218 输出: false 解法一&#xff1a; class Solution { public:bool isPowerOfTwo(int n) {return(n >…

C库函数

Linux的系统I/O函数&#xff08;read、write、open、close和 lseek等&#xff09;与C语言的C库函数&#xff08;libc.so库文件中&#xff09;都是相对应的&#xff0c;它们都是动态库函数。如下图所示&#xff0c;C库函数有fopen、fclose、fwrite、fread和fseek等。这些C库函数…

【Leetcode | 48】226. 翻转二叉树

翻转一棵二叉树。 示例&#xff1a; 输入&#xff1a; 4 / \ 2 7 / \ / \ 1 3 6 9 输出&#xff1a; 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell 的 原问题 启发的 &#xff1a; 谷歌&#xff1a;我们90&#xff05;的…

C库函数与Linux系统函数之间的关系

由上小节知道&#xff0c;C库函数是借助FILE类型的结构体来对文件进行操作的&#xff0c;其本身只是在用户空间&#xff08;I/O缓冲区&#xff09;进行读写操作&#xff0c;而数据在内核与用户空间之间的传递、以及将内核与I/O设备之间的数据传递都是该C库函数进行一系列的系统…

【第十六章】模板实参推断

二、模板显式推断 在C中&#xff0c;若函数模板返回类型需要用户指定&#xff0c;那么在定义函数模板时&#xff0c;模板参数的顺序是很重要的&#xff0c;如下代码&#xff1a; template <typename T1, typename T2, typename T3> //模板一 T1 sum(T2 a, T3 b) {retu…

open函数和errno全局变量

&#xff08;1&#xff09;open函数 man man 查看man文档的首页 其中DESCRIPTION部分描述了man文档的每一章的章节内容 第2章System calls为系统调用&#xff0c;即Liunx系统函数。 man 2 open 查看第二章的open函数的详细帮助文件。 open函数用于打开一个已经的文件或者创…

open函数和close函数的使用

学习几个常用的Linux系统I/O函数&#xff1a;open、close、write、read和lseek。注意&#xff0c;系统调用函数必须都考虑返回值。 &#xff08;1&#xff09;open函数的使用 首先&#xff0c;需要包含三个头文件&#xff1a;<sys/types.h> <sys/stat.h> <…

【Leetcode | 9】217. 存在重复元素

解题代码&#xff1a; bool containsDuplicate(vector<int>& nums) {return nums.size() > set<int>(nums.begin(), nums.end()).size(); }

全缓冲、行缓冲和无缓冲

这里的缓冲是指的是用户空间的I/O缓冲区&#xff0c;不是内核缓冲。 无缓冲&#xff1a;用户层不提供缓冲&#xff0c;数据流直接到内核缓冲&#xff0c;再到磁盘等外设上。标准错误输出&#xff08;2&#xff09;通常是无缓存的&#xff0c;因为它必须尽快输出&#xff0c;且…

【Leetcode】1. 两数之和

给定一个整数数组 nums 和一个目标值 target&#xff0c;请你在该数组中找出和为目标值的那 两个 整数&#xff0c;并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;你不能重复利用这个数组中同样的元素。 示例: 给定 nums [2, 7, 11, 15], targ…

read和write函数的使用

都需要包含头文件&#xff1a; <unistd.h> read系统函数从打开的设备或文件中读取数据&#xff0c;即将数据从外设上经过内核读到用户空间&#xff1b;write系统函数相反&#xff0c;向打开的设备或文件中写入数据&#xff0c;即将数据从用户空间&#xff08;I/O缓冲&am…

1091. Acute Stroke (30)

One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core. Input Speci…

lseek函数的使用

需要包含头文件&#xff1a;<sys/types.h> <unistd.h> off_t lseek(int fd, off_t offset, int whence)&#xff1b; 函数原型 函数功能&#xff1a;移动文件读写指针&#xff1b;获取文件长度&#xff1b;拓展文件空间。 在使用该函数之前需要将文件打开&…

19. 删除链表的倒数第N个节点

给定一个链表&#xff0c;删除链表的倒数第 n 个节点&#xff0c;并且返回链表的头结点。 示例&#xff1a; 给定一个链表: 1->2->3->4->5, 和 n 2. 当删除了倒数第二个节点后&#xff0c;链表变为 1->2->3->5. 说明&#xff1a; 给定的 n 保证是有效的。…

文件操作相关的系统函数

重点学习&#xff1a;stat&#xff08;fstat、lstat 获取文件属性&#xff09;、access&#xff08;测试指定文件是否拥有某种权限&#xff09;、chmod&#xff08;改变文件的权限&#xff09;、chown&#xff08;改变文件的所属主和所属组&#xff09;、truncate&#xff08;截…

stat函数(stat、fstat、lstat)

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> //需包含头文件 有如下三个函数的函数原型&#xff1a; int stat(const char *path, struct stat *buf); 第一个形参&#xff1a;指出文件&#xff08;文件路径&#xff09;&…

1062. Talent and Virtue (25)

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about peoples talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage&#xff08;圣人&#xff09;"…

access、strtol函数的使用(后者为C库函数)

#include <unistd.h> int access(const char *pathname, int mode); 作用&#xff1a;检查调用该函数的进程是否可以对指定的文件执行某种操作。 第一个形参&#xff1a;文件名&#xff1b;第二个形参&#xff1a;R_OK&#xff08;是否可读&#xff09;、W_OK&#xf…