02-线性结构3 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 (≤) which is the total number of nodes, and a positive K (≤) 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 = 100010;
struct Node{int address,data,next;int flag;
}node[maxn];bool cmp(Node a,Node b){return a.flag < b.flag;
}//void print(int i,int k,int n){
//    int j;
//    for(j = i + k - 1; j >= i; j--){
//        printf("%05d %d ",node[j].address,node[j].data);
//        if(j > i){
//            printf("%05d\n",node[j-1].address);
//        }else{
//            if(i+2*k<=n){
//                printf("%05d\n",node[i+2*k].address);
//            }
//        }
//    }
//}int main(){for(int i = 0 ; i < maxn; i++){node[i].flag = maxn;}int n,k,begin;scanf("%d%d%d",&begin,&n,&k);int address;for(int i = 0; i < n; i++){scanf("%d",&address);scanf("%d%d",&node[address].data,&node[address].next);node[address].address = address;//node[address].flag = maxn;
    }int count = 0,p = begin;while(p!=-1){node[p].flag = count++;p = node[p].next;}sort(node,node+maxn,cmp);    n = count;    for(int i = 0; i < n/k; i++){for(int j = (i+1)*k-1; j > i*k; j--){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");}}}}//    if(n == 1){
//        printf("%05d %d -1\n",node[0].address,node[0].data);
//        return 0;
//    }
//    bool flag = true;
//    if(count%k!=0){
//        while(count%k!=0) count--;
//        flag = false;
//    }
//    for(int i = 0; i < count; i += k){
//        print(i,k,count);
//    }
//    
//    if(flag == true){
//        printf("-1\n");
//    }else{
//        printf("%05d\n",node[count].address);
//        for(int i = count; 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");
//        }
//    }
//    
//    for(int i = 0; i < n; i++){
//        printf("%05d %d\n",node[i].address,node[i].data);
//    }return 0;
}

 

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

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

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

相关文章

03-树1 树的同构 (25 分)

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2&#xff0c;则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的&#xff0c;因为我们把其中一棵树的结点A、B、G的左右孩子互换后&#xff0c;就得到另外一棵树。而图2就不是同构的。 图1 图2 现给定两棵…

【树】104. 二叉树的最大深度

题目 104. 二叉树的最大深度 方法1 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr),…

Leetcode 206. 反转链表

206. 反转链表 解法1 class Solution { public:ListNode *reverseList(ListNode *head){if(!head || !head->next)return head;ListNode *p;p reverseList(head->next);head->next->next head;head->next nullptr;return p;} };解法2 /*** Definition for…

05-树7 堆中的路径 (25 分)

将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i&#xff0c;打印从H[i]到根结点的路径。 输入格式: 每组测试第1行包含2个正整数N和M(≤)&#xff0c;分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初…

Leetcode 124.二叉树中的最大路径

解法1 解法 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* …

《UNIX环境高级编程》目录

第一章&#xff1a;UNIX标准及实现 01 函数perror、strerror 第三章&#xff1a;文件I/O 01 C库函数 02 文件描述符、函数open和openat 03 函数read、write、lseek 04 函数dup和dup2 第四章&#xff1a;文件和目录 01 函数stat、fstat、fstatat和lstat 02 函数umask 03 函…

06-图1 列出连通集 (25 分)

给定一个有N个顶点和E条边的无向图&#xff0c;请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时&#xff0c;假设我们总是从编号最小的顶点出发&#xff0c;按编号递增的顺序访问邻接点。 输入格式: 输入第1行给出2个整数N(0)和E&#xff0c;分别是图的…

牛客网算法题题解

序号题目语言标记1 C解题报告 2 3 4字符串归一化C解题报告

06-图3 六度空间 (30 分)

“六度空间”理论又称作“六度分隔&#xff08;Six Degrees of Separation&#xff09;”理论。这个理论可以通俗地阐述为&#xff1a;“你和任何一个陌生人之间所间隔的人不会超过六个&#xff0c;也就是说&#xff0c;最多通过五个人你就能够认识任何一个陌生人。”如图1所示…

Linux网络编程目录

UNIX网络编程目录 1. TCP三次握手的第三次的 ack包丢失会怎样&#xff1f; 2. inux网络编程“惊群”问题总结

Linux高性能服务器编程

一、文件IO、标准IO 1. 2. 函数dup和dup2 三、进程 1. fork、vfork、clone 2. 函数wait、waitpid、孤儿进程、僵尸进程 3. 进程组 4. 会话 四、信号 1. 函数signal、sigaction 2. 函信号SIGCHLD 3. 函数kill、raise、abort、alarm 4. 信号集、sigprocmask、sigpending 五、…

07-图4 哈利·波特的考试 (25 分)

哈利波特要考试了&#xff0c;他需要你的帮助。这门课学的是用魔咒将一种动物变成另一种动物的本事。例如将猫变成老鼠的魔咒是haha&#xff0c;将老鼠变成鱼的魔咒是hehe等等。反方向变化的魔咒就是简单地将原来的魔咒倒过来念&#xff0c;例如ahah可以将老鼠变成猫。另外&…

C++ Primer (二)目录

第十五章&#xff1a;面向对象程序设计 1. 虚函数/2. 虚函数表剖析&#xff08;一&#xff09;3. 虚函数表剖析&#xff08;二&#xff09;4. 虚函数表剖析&#xff08;三&#xff09;

07-图6 旅游规划 (25 分)

有了一张自驾旅游路线图&#xff0c;你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序&#xff0c;帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的&#xff0c;那么需要输出最便宜的一条路径。 输入格式: 输…

08-图7 公路村村通 (30 分)

现有村落间道路的统计数据表中&#xff0c;列出了有可能建设成标准公路的若干条道路的成本&#xff0c;求使每个村落都有公路连通所需要的最低成本。 输入格式: 输入数据包括城镇数目正整数N&#xff08;≤&#xff09;和候选道路数目M&#xff08;≤&#xff09;&#xff1b;随…

《回溯算法》目录

序号题目标记1 46. 全排列 2 47. 全排列 II 3 39. 组合总和 4 40. 组合总和 II 5 6

08-图9 关键活动 (30 分)

假定一个工程项目由一组子任务构成&#xff0c;子任务之间有的可以并行执行&#xff0c;有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。 比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成…

C++ Primer

C Primer目录索引 虚函数表剖析&#xff08;一&#xff09;虚函数表剖析&#xff08;二&#xff09;static关键字用法 volatile、const的用法​​​​​​​

10-排序4 统计工龄 (20 分)

给定公司N名员工的工龄&#xff0c;要求按工龄增序输出每个工龄段有多少员工。 输入格式: 输入首先给出正整数N&#xff08;≤&#xff09;&#xff0c;即员工总人数&#xff1b;随后给出N个整数&#xff0c;即每个员工的工龄&#xff0c;范围在[0, 50]。 输出格式: 按工龄的递…

10-排序6 Sort with Swap(0, i) (25 分)

Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the follow…