原题连接: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. swift 3.0 协议笔记

    协议能够要求遵循者必须含有一些特定名称和类型的实例属性(instance property)或类属性(type property),也能够要求属性的(设置权限)settable 和(访问权限)gett ...

  2. zookeeper3.3.6 伪分布式安装

    下载地址(http://zookeeper.apache.org/releases.html#download)   一:下载zookeeper的安装包,解压,进入到zk的目录文件,进入conf目录 ...

  3. 【转】理解 PHP 依赖注入 | Laravel IoC容器

    Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据 ...

  4. 【Java EE 学习 69 上】【struts2】【paramsPrepareParamsStack拦截器栈解决model对象和属性赋值冲突问题】

    昨天有同学问我问题,他告诉我他的Action中的一个属性明明提供了get/set方法,但是在方法中却获取不到表单中传递过来的值.代码如下(简化后的代码) public class UserAction ...

  5. postgresql 常用数据库命令

    连接数据库, 默认的用户和数据库是postgrespsql -U user -d dbname 切换数据库,相当于MySQL的use dbname\c dbname列举数据库,相当于mysql的sho ...

  6. 简单爬虫,突破IP访问限制和复杂验证码,小总结

    简单爬虫,突破复杂验证码和IP访问限制 文章地址:http://www.cnblogs.com/likeli/p/4730709.html   好吧,看题目就知道我是要写一个爬虫,这个爬虫的目标网站有 ...

  7. RSA密钥之C#格式与Java格式转换

    前言 最近由于项目需求,服务端由c#编写,客户端由java编写.通信数据使用RSA非对称加密.但是java和c#生成的密钥格式是不一样的,所以需要转换格式才可以正常使用.网上搜到使用java进行格式转 ...

  8. 隐藏进程中的模块绕过IceSword的检测

    标 题: [原创] 隐藏进程中的模块绕过IceSword的检测 作 者: xPLK 时 间: 2008-06-19,17:59:11 链 接: http://bbs.pediy.com/showthr ...

  9. 2.View绘制分析笔记之onMeasure

    今天主要学习记录一下Android View绘制三部曲的第一步,onMeasure,测量. 起源 在Activity中,所有的View都是DecorView的子View,然后DecorView又是被V ...

  10. 学习微信小程序之css9内边距

    padding内边距 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...