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 ...
随机推荐
- VC++ : error LNK2005: ... already defined in *.obj
今天写代码遇到了这么一个链接错误:"已经在*.obj中定义". error LNK2005: "void __cdecl ReplaceWstringVar(class ...
- [转]阿里巴巴数据库连接池 druid配置详解
一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...
- javascript 核心语言笔记 5 - 语句
表达式在 JavaScript 中是短语(phrases),那么语句(statements)就是 JavaScript 整句或命令,语句以分号结束.表达式计算出一个值,语句用来执行以使某件事情发生 表 ...
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...
- Oracle over函数
Oracle over函数 SQL code: sql over的作用及用法RANK ( ) OVER ( [query_partition_clause] order_by_clause )DE ...
- oracle内存粒度
一,什么是内存粒度? When a database instance starts up, the amount of memory allocated is determined by the a ...
- 【leetcode】Largest Number
题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...
- EF不能很好的支持DDD?估计是我们搞错了!
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:最近在ABP项目中尝试纯粹的DDD,然后遇到EF实现的Repository似乎不能很好 ...
- linux内核(kernel)版本号的意义
转自:http://www.cnblogs.com/jsjliuxing/archive/2011/12/01/2271182.html 在linux下有一个目录,即/usr/src/kernels/ ...
- 3 3Sum closest_Leetcode
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...