原题连接: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. SDWebImage清理图片缓存方法

    //获取当前缓存大小 @property (nonatomic, assign) NSUInteger tmpSize; //获取缓存大小并储存 _tmpSize=[[SDImageCache sha ...

  2. java项目上线过程

    关于如何将Javaweb上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. 1.在myeclipse中开发好项目,打包成war格式,不会的同学参考以下 http://z ...

  3. Shell $? $* $@ 等含义

    $0 ------>脚本名 $1 ------>传入的第一个参数值 $? ------>脚本执行的结果.成功==0,不成功==非0 $* ------>所有参数的内容 $@ - ...

  4. gulp整理

    gulp基于node 1.全局安装gulp: $ npm install --global gulp 2.前往项目目录,然后安装作为项目的开发依赖(devDependencies): $ npm in ...

  5. lua相关笔记

    --[[ xpcall( 调用函数, 错误捕获函数 ); lua提供了xpcall来捕获异常 xpcall接受两个参数:调用函数.错误处理函数. 当错误发生时,Lua会在栈释放以前调用错误处理函数,因 ...

  6. tp框架获取常量信息、方法、命名空间

    获取系统常量信息: public function ShowInFo() { var_dump(get_defined_constants(true)); //如果参数为true,则分类显示 } 在这 ...

  7. centos下建立双机信任关系

    在有些情况下,我们希望在两台centos机器之间建立ssh连接的时候,可以不用输入密码.最常见的情况就是在使用脚本做数据库备份的时候.这种情况下,我们可以通过公钥/私钥来建立双机之间的信任关系. 网上 ...

  8. C#做上位机软件——绘图并传输给下位机

    拿到任务之后首先分成了几个部分: 1.绘图.学习了GDI+ 2.图片保存. 3.将图片转换成byte[].由于使用Socket通信,只能传输byte[]数据,所以这一步是向下位机传输的关键. 相应地, ...

  9. [BI项目记]-BUG创建

    BUG是在项目过程中以及运维过程中经常遇到的工作项.在处理每一个BUG的过程中,通过项目管理系统把BUG相应的内容纪录下来也是很重要的.这里将介绍如何通过TFS来完成BUG的创建工作. 首先我们来看B ...

  10. Jquery的$(selector).each()和$.each()原理和区别

    我们都用过Jqurey中的each函数,都知道each()有两种方式去调用,一种是通过$.each()调用,另一种是$(selector).each()去调用,那么它们之间有什么区别? 翻看一下Jqu ...