原题连接:https://www.patest.cn/contests/pat-a-practise/1074

题目:

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<stdio.h>
#define Max 100000
typedef struct Node {
int Data;
int Next;
}node;
/* 统计节点的个数 */
int CountNodes(node *list,int Plist) //因为题目中有多余的节点,故要统计节点个数
{
int cnt=;
while((Plist=list[Plist].Next)!=-)cnt++;
return cnt;
}
/*输入函数 */
void Input(node *list,int n,int Plist)
{
int i,addr,Data,Next;
for (i=;i<=n;i++) //利用数组模拟节点!
{
scanf("%d%d%d",&addr,&Data,&Next); //不必按照顺序输入,是因为只要有“头节点”,就可以找到其他元素了
list[addr].Data=Data;
list[addr].Next=Next; //输入完成后,即形成了一个单向链表
}
}
/* 逆序函数 */
int Reverse(node *list,int Plist,int cnt,int k)
{
int prevNode,currNode,nextNode; //三个基本要素
prevNode=-;
currNode=Plist;
nextNode=list[currNode].Next;
int i,j;
int lasthead,head=-;
for (j=;j<cnt/k;j++) //分为 cnt/k 段进行逆序,每段k个节点 。后面的余数不必逆序
{
lasthead=head; //前一段逆序好的末尾节点
head=currNode; //逆序好的该段的末尾节点
for (i=;i<k;i++)
{ // 单链表逆序的基本模板!
list[currNode].Next=prevNode;
prevNode=currNode;
currNode=nextNode;
nextNode=list[nextNode].Next;
}
if (j==)Plist=prevNode; //整个逆序好的链表的第一个节点
else list[lasthead].Next=prevNode; //前一段的尾节点和其下一段的头节点 对上!
}
list[head].Next=currNode; //进行逆序的最后一段的末尾节点和剩余没有进行逆序的段的头节点 对上!
return Plist;
}
void Printlist(node *list,int Plist)
{
while((list[Plist].Next)!=-){
printf("%05d %d %05d\n",Plist,list[Plist].Data,list[Plist].Next);
Plist=list[Plist].Next;}
printf("%05d %d %d\n",Plist,list[Plist].Data,list[Plist].Next);
}
int main()
{
int Plist,n,k;
node list[Max];
scanf("%d%d%d",&Plist,&n,&k);
Input(list,n,Plist);
int cnt;
cnt=CountNodes(list,Plist);
Plist=Reverse(list,Plist,cnt,k);
Printlist(list,Plist);
return ;
}

Reversing Linked List的更多相关文章

  1. PAT1074 Reversing Linked List (25)详细题解

    02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

  3. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  4. 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...

  5. 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 ...

  6. 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 ...

  7. PAT_A1074#Reversing Linked List

    Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...

  8. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  9. 想了很久,一道Microsoft的笔试题目 —— Reversing Linked List

    Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the ...

  10. 02-线性结构2 Reversing Linked List

    由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...

随机推荐

  1. DateUtil(SimpleDateFormat)

    import java.util.Calendar; import java.util.Date; import java.text.SimpleDateFormat; public class Da ...

  2. ecshop不同文章分类调用不同文章分类模板

    根据需要,不同的文章分类会有不一样的页面风格.也就是说根据文章分类ID来判断,输出不同的文章分类模板. 重点就是文章分类的ID. 打开:article_cat.php $smarty->disp ...

  3. python 旋转数组

    #!/usr/bin/env python3 #-*-encoding:utf-8-*- l = [] u = [] q = 5 xx=[[col for col in range(q)] for r ...

  4. react开发环境搭建

    ---恢复内容开始--- 要想用react,需要安装: 1)babel-sublime: 作用:编译es6,支持ES6, React.js, jsx代码高亮,并对所编译的代码进行高亮显示. 安装步骤: ...

  5. iOS动画效果和实现

    动画效果提供了状态或页面转换时流畅的用户体验,在iOS系统中,咱们不需要自己编写绘制动画的代码,Core Animation提供了丰富的api来实现你需要的动画效果. UIKit只用UIView来展示 ...

  6. JS脚本语言是什么意思?

    javascript,Javascript是一种浏览器端的脚本语言,用来在网页客户端处理与用户的交互,以及实现页面特效.比如提交表单前先验证数据合法性,减少服务器错误和压力.根据客户操作,给出一些提升 ...

  7. JAVA图片验证码

    package hh.com.util; import java.io.IOException; import javax.servlet.ServletException; import javax ...

  8. AngularJS 相关小问题解决方案合集

    1  解决 Select选择框遍历时,出现一个空白选项: <select style="width: 20%;margin-left: 5px;height: 31px;" ...

  9. *** wechat-php-sdk 微信公众平台php开发包

    wechat-php-sdk 微信公众平台php开发包,细化各项接口操作,支持链式调用,欢迎Fork此项目weixin developer SDK. 项目地址:https://github.com/d ...

  10. 父元素相对定位后,子元素在ie下被覆盖的问题!

    <div id="append_parent" style="position: relative;"> <div id="zoom ...