PAT乙级真题及训练题 1025. 反转链表 (25)


感觉几个世纪没打代码了,真是坏习惯,调了两小时把反转链表调出来了,心情舒畅。

这道题的步骤

  • 数据输入,数组纪录下一结点及储存值
  • 创建链表并储存上下结点“位置”
  • 按照规律遍历一遍链表并反转链表
  • 输出整条反转完的链表
#include <iostream>
#include <cstdio>
using namespace std;
int nextnum[100010],value[100010];
struct Node{
int value,pos,nnext;
Node *pnext,*plast,*final;
};
int main() {
int start,n,k,pos,next,val,count=1; /*-------------数据输入,数组纪录下一结点及储存值-------------*/
scanf("%d%d%d",&start,&n,&k);
for (int i=0; i<n; i++) {
scanf("%d%d%d",&pos,&val,&next);
nextnum[pos]=next;
value[pos]=val;
}
/*------------------------------------------------------*/ /*-------------创建链表并储存上下结点“位置”-----------------*/
pos=start;
Node *p1,*p2,*head;
p1=p2=new Node;
p1->value=value[pos];
p1->pos=pos;
head=p1;
pos=nextnum[pos];
while (pos!=-1) {
p1=new Node;
p2->pnext=p1;
p1->plast=p2;
p1->value=value[pos];
p1->pos=pos;
p2=p1;
pos=nextnum[pos];
count++;
}
p1->pnext=NULL; if (count==1) {
printf("%05d %d -1\n",head->pos,head->value);
return 0;
}
/*------------------------------------------------------*/ /*-------------反转链表----------------------------------*/
int cur=0;
Node *temp,*temp1=NULL;
temp=head;
p1=head;
while (p1) {
if (cur==k-1) {
head=p1;
}//find the head if (cur%k==0 && cur<count/k*k) {
p1->nnext=-1;
p1->final=NULL;
if (cur%(2*k)==0) temp=p1;
else temp1=p1;
}else {
if (cur<count/k*k) {
p1->final=p1->plast;
p1->nnext=p1->final->pos;
}else {
p1->final=p1->pnext;
if (cur<count-1) p1->nnext=p1->final->pos;
else p1->nnext=-1;
}
} if (cur>k-1 && (cur+1)%k==0) {
if ((cur+1)%(2*k)==0) {
temp->final=p1;
temp->nnext=p1->pos;
}else {
temp1->final=p1;
temp1->nnext=p1->pos;
} }
if (cur==count/k*k) {
if (cur%(2*k)!=0) {
temp->final=p1;
temp->nnext=p1->pos;
}else {
temp1->final=p1;
temp1->nnext=p1->pos;
} } cur++;
p1=p1->pnext;
if (cur==count) {
break;
}
}
/*------------------------------------------------------*/ /*-------------输出整条反转完的链表------------------------*/
p1=head;
while (p1) {
if (p1->nnext==-1) printf("%05d %d %d\n",p1->pos,p1->value,p1->nnext);
else printf("%05d %d %05d\n",p1->pos,p1->value,p1->nnext);
p1=p1->final;
} return 0;
}

另贴出一两个月前用指针结合数组的做法做的,作为对比

#include <iostream>
#include <cstdio>
using namespace std;
struct Node{
Node * pointer;
int nnext;
int position;
int nvalue;
};
Node line[100010],input[100010];
int main(){
int first,n,mol,
num,value,next,
i;
cin >> first >> n >> mol;
if (n==1) {
cin >> num >> value >> next;
printf("%05d %d -1\n",num,value);
}else{
for (i=0; i<n; i++) {
cin >> num >> value >> next;
if (next == -1) next=100002;
input[num].nvalue = value;
input[num].nnext = next;
input[num].position = num;
if (num == first) line[0] = input[num];
}
for (i=1; i<n; i++) {
line[i]=input[line[i-1].nnext];
if (line[i].nnext == 100002) break;
}
n=i+1;
int nreverse=(n/mol)*mol;
for (i=0; i<nreverse; i++) {
if (i%mol!=0) line[i].pointer = &line[i-1];
else if (i==nreverse-mol) line[i].pointer = &line[nreverse];
else line[i].pointer = &line[i+2*mol-1];
line[i].nnext = line[i].pointer->position;
}
for (i=nreverse; i<n-1; i++) line[i].pointer = &line[i+1];
if (n%mol == 0) {
line[n-mol].pointer = NULL;
line[n-mol].nnext = 100002;
}
else line[n-1].pointer = NULL;
Node * p= &line[mol-1];
if (n<mol) p = &line[0];
while (p != NULL) {
if (p->nnext != 100002) printf("%05d %d %05d\n",p->position,p->nvalue,p->nnext);
else printf("%05d %d -1\n",p->position,p->nvalue);
p = p->pointer;
}
}
return 0;
}

PAT乙级真题及训练题 1025. 反转链表 (25)的更多相关文章

  1. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  2. PAT (Basic Level) Practice (中文)1025 反转链表 (25分)

    1025 反转链表 (25分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→ ...

  3. PAT乙级 1025. 反转链表 (25)

    1025. 反转链表 (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个常数K以及一个单链表L,请 ...

  4. PAT 1025 反转链表 (25)(STL-map+思路+测试点分析)

    1025 反转链表 (25)(25 分) 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4:如果K为4, ...

  5. PAT-乙级-1025. 反转链表 (25)

    1025. 反转链表 (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个常数K以及一个单链表L,请 ...

  6. PAT 1025. 反转链表 (25)

    给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5→6,即最后 ...

  7. 1025 反转链表 (25 分)C语言

    题目描述 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为 3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5 ...

  8. 1054. 求平均值 (20)-PAT乙级真题

    今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...

  9. PAT乙级真题1003. 我要通过!(20)(解题)

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...

随机推荐

  1. Java设计模式学习记录-简单工厂模式、工厂方法模式

    前言 之前介绍了设计模式的原则和分类等概述.今天开启设计模式的学习,首先要介绍的就是工厂模式,在介绍工厂模式前会先介绍一下简单工厂模式,这样由浅入深来介绍. 简单工厂模式 做法:创建一个工厂(方法或类 ...

  2. syslog - 日志文件详解

    日志文件,是linux最为重要的记录文件,记录着日常的操作. 我们在linux编程的时候,通常会使用日志文件记录操作和信息,日志系统提供了我们几个API接口供调用 1. API void openlo ...

  3. C# ABP 配置连接数据库&创建表

    1. 配置连接数据库 配置连接数据库很简单,只需要打开Web项目,然后找到Web.config,配置如下: <connectionStrings> <add name="D ...

  4. [转]sp_OACreate WriteLine Writing nvarchar 中文汉字 非乱码to a text file

    本文转自:https://stackoverflow.com/questions/48135889/writing-nvarchar-to-a-text-file According to the S ...

  5. sqlserver查询连续签到天数

    create table #t(keyId int identity,actionDate datetime)insert into #t(actionDate) select distinct Cr ...

  6. MFC获取系统信息

    一.获取系统时间 CString str,str2; CTime time; time = CTime::GetCurrentTime(); str = time.Format("%Y年%m ...

  7. 微信小程序开源Demo精选

    来自:http://www.jianshu.com/p/0ecf5aba79e1 文/weapphome(简书作者)原文链接:http://www.jianshu.com/p/0ecf5aba79e1 ...

  8. UML,构件图与部署图

    一.构件图概述 1.概念 用来显示一组构件之间的组织及其依赖关系 2.基本元素 (1)构件:定义了良好接口的物理实现单元. ● 配置构件:形成可执行文件的基础,如:动态链接库(DLL).ActiveX ...

  9. Java基础——Servlet(二)

    好久没有写博客了,最近有在学习.可能是遇到瓶颈了,学到Servlet这里觉得有些吃力.前几天已经学完一遍了,但是学完之后觉得还是很迷茫.去知乎和百度上搜索,遇到的都是一些概念之类的讲解.核心的介绍说, ...

  10. HDU2444(KB10-B 二分图判定+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...