PTA 02-线性结构3 Reversing Linked List (25分)
题目地址
https://pta.patest.cn/pta/test/16/exam/4/question/664
5-2 Reversing Linked List (25分)
Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL being 1→2→3→4→5→6, if K = 3K=3, then you must output 3→2→1→6→5→4; if K = 4K=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 NN (\le 10^5≤105) which is the total number of nodes, and a positive KK (\le N≤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 NN 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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 15:54 答案正确 25 5-2 gcc 131 3
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 12/12 2 1
测试点2 答案正确 3/3 14 1
测试点3 答案正确 2/2 1 1
测试点4 答案正确 2/2 2 1
测试点5 答案正确 2/2 2 1
测试点6 答案正确 3/3 131 3
测试点7 答案正确 1/1 2 1
*/
#include<stdio.h>
#define MAXLEN 100002
struct node {
int data;
int next;
}; int k,head; struct node workArray [MAXLEN]; int Input(struct node array[])
{
int i,inputHead,inputLength;
int index,data,next; scanf("%d %d %d",&inputHead,&inputLength,&k);
for (i=0;i<inputLength;i++){
scanf("%d %d %d",&index,&data,&next);
array[index].data=data;
array[index].next=next;
}
return inputHead;
} int count(int head,struct node array[])
{
int i,cnt=1;
i=head;
while(array[i].next!=-1){
cnt++;
i=array[i].next;
}
return cnt;
}
void PrintList(int head,struct node array[])
{
int idx=head;
while(array[idx].next!= -1){
printf("%05d %d %05d\n",idx,array[idx].data,array[idx].next);
idx=array[idx].next;
}
printf("%05d %d %d",idx,array[idx].data,array[idx].next);
} int ReverseList(struct node array[],int *head,int k)
{
/*
首先用count求链表长度,放到cnt中保存。每次执行cnt自身减掉k,如果cnt<k则不进行翻转
然后使用ptr1 ptr2 ptr3 三个指针
ptr1为当前节点 ptr2为下一个节点 将ptr1->ptr2改为ptr2->ptr1。因为ptr2中的next原有内容会丢失,故用ptr3保存ptr2的下一个节点
执行完一次后,k个节点区间内,头尾互换。
故lastend保存前一区块的末端,是上一区间的头节点
nexthead即下一区块的头结点,同样也是该区块翻转完后的末端。于是提前用lastend=nexthead保存。
*/
int cnt;
if(k==1)
return;
cnt=count(*head,array);
int i,ptr1,ptr2,ptr3,firstflag=0,nexthead=*head,lastend=-2;//ptr1指当前指针,ptr2指下一个要指向ptr1的,ptr3指向还未做反转的下一个。
while(cnt>=k){
// printf("-------head=%d,nexthead=%d,cnt=%d\n",*head,nexthead,cnt);//for_test
ptr1=nexthead;
ptr2=array[ptr1].next;
for(i=1;i<k;i++){
ptr3=array[ptr2].next;
array[ptr2].next=ptr1;
ptr1=ptr2;
ptr2=ptr3; } array[nexthead].next=ptr3;//主要反转做完后,重新定义头尾节点的指向。
if(firstflag==0){
lastend=nexthead;
*head=ptr1;//因为在循环中最后改变了ptr2的值,所以此处用ptr1 。 }
else{
array[lastend].next=ptr1;
lastend=nexthead;
} firstflag++;
nexthead=ptr2;
cnt-=k;
}
} int main()
{ head=Input(workArray);
ReverseList(workArray,&head,k);
PrintList(head,workArray);
}
PTA 02-线性结构3 Reversing Linked List (25分)的更多相关文章
- 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 elem ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- [刷题] PTA 02-线性结构3 Reversing Linked List
链表逆序 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namesp ...
- 数据结构练习 02-线性结构2. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
随机推荐
- 正则表达式中的?=,?!,?<=,?<!(预查)解释小栗子
之前在学正则表达式的时候学的并不是很透彻 感觉看看元字符(元字符要用 \ 转义),限定符(^开头 $结尾), 前面写个范围[a-z],在后面写个{n,}能匹配就行了 当时的自己 然而昨天我参加了个 ...
- 关于Java虚拟机
先占个坑,可以参考以下两篇文档来进行初步的学习 http://www.cnblogs.com/fingerboy/p/5456371.html http://www.importnew.com/244 ...
- codevs 1422 河城荷取
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 在幻想乡,河城荷取是擅长高科技工业的河童.荷取的得意之作除了光学迷彩外,还有 ...
- 为什么ABAP整型的1转成string之后,后面会多个空格
有同事问这个问题:lv_s是从int4转过来的,长度为2,和硬编码的lv_s2(长度为1)相比,后面多了个空格. 为什么?查SAP ABAP的编程帮助F1 help. 帮助文档说的很清楚了:如果赋值操 ...
- 聊天室(C++客户端+Pyhton服务器)3.群功能添加
创建群 数据库 group_table(user, name) grpuser_table(grpname,user) 按下添加群按钮 // 创建群组void CUserDialog::OnBnCli ...
- df - 报告文件系统磁盘空间的使用情况
总览 df [OPTION]... [FILE]... POSIX 选项: [-kP] GNU 选项 (最短方式): [-ahHiklmPv] [-t fstype] [-x fstype] [--b ...
- python之bool (布尔值)
用途: 判断真假 识记: 空的字符串是False,非空的就是True 0 是False,非0的都是True 格式: True False 布尔值转换成字符串: print(type(str(Tru ...
- CSS中列表项list样式
CSS列表属性 属性 描述 list-style-属性 用于把所有用于列表的属性设置于一个声明中. list-style-image 将图象设置为列表项标志. list-style-position ...
- 洛谷——P1627 [CQOI2009]中位数
P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...
- [kuangbin带你飞]专题五 并查集
并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...