Reversing Linked List
原题连接: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的更多相关文章
- PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 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 ...
- 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 ...
- 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 ...
- PAT_A1074#Reversing Linked List
Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
- 想了很久,一道Microsoft的笔试题目 —— Reversing Linked List
Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the ...
- 02-线性结构2 Reversing Linked List
由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...
随机推荐
- Windows 网络编程
网络编程 API ,失败返回 -,错误代码 WSASYSNOTREADY 表示基础网络子系统没有准备好网络通行,WSAVERNOTSUPPORTED 表示 Socket 版本不支持,WSAEINPRO ...
- win7系统网卡驱动正常,网线连接设备正常,但电脑右下角网络图片显示一直在转圈或者显示一个黄色感叹号的解决办法
今天遇到一个问题是电脑的win7系统一直都可以连接有线,但今天突然连接不了.在我的电脑右键-->管理--->设备管理器-->网络适配器,里面查看了网络适配器安装正常.但是电脑右下角的 ...
- kettle系列-kettle管理平台部署说明
本介绍我的开源项目[kettle-manager]kettle管理平台如何获取并部署使用,该项目介绍请参看另一篇博文:http://www.cnblogs.com/majinju/p/5739820. ...
- 使用 jsoup 对 HTML 文档进行解析和操作
jsoup 简介 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HT ...
- llvm-summary
llvm 学习总结 Type define int类型 IntegerType::get(mod->getContext(), 32) long类型 IntegerType::get(mod-& ...
- struts 2 时间控件
在使用struts2框架时,为我们提供了时间选择器控件:datetimepicker.但是在使用过程中会出现一些问题,主要就是struts2版本更新时做了一些修改.在struts2.0时,使用< ...
- C#运算符号
double x=5.1e3;// 5.1乘以10 的3次方. x就是 5100 //注 : 5.1e+3=5.1e3=5.1e03=5.1e+03 double y=5.1e-3;// 5.1乘以 ...
- 常见css水平自适应布局
左右布局,左边固定,右边自适应布局 BFC方法解决 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 【大数据技巧】日均2TB日志数据在线快速处理之法
[大数据技巧]日均2TB日志数据在线快速处理之法 http://click.aliyun.com/m/8958/
- 获取终端ip地址
网上找的,记录下 import java.io.*; import java.net.*; import java.util.*; //import org.apache.http.conn.util ...