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. angular项目使用Swiper组件Loop时 ng-click点击事件失效处理方法

    在Angular项目中,使用swiper组件进行轮播展示时,存在将swper的loop设置为true时,部分页面的ng-click失效. 原因:将swiper中的looper设置为true时,为了视觉 ...

  2. git 撤销add和commit

    有时候改完代码发现改错分支了,而这个时候已经add或者commit了,怎么办,有办法: 1.若果已经add .  了这个时候可以使用git stash命令,具体操作命令如下: (1) > git ...

  3. Java 支付宝支付,退款,单笔转账到支付宝账户(支付宝订单退款)

    上一篇写到支付宝的支付,这代码copy下来就能直接用了,   我写学习文档时会经常贴 官方参数文档的案例地址, 因为我觉得 请求参数,响应参数说明 官方文档整理的很好,毕竟官方不会误导大家. 我学一个 ...

  4. 两车追及或相遇问题(hdu1275)数学题

    两车追及或相遇问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. N皇后问题hdu2553(dfs)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. Java虚拟机 - 类初始化

    [深入Java虚拟机]之三:类初始化 类初始化是类加载过程的最后一个阶段,到初始化阶段,才真正开始执行类中的Java程序代码.虚拟机规范严格规定了有且只有四种情况必须立即对类进行初始化: 遇到new. ...

  7. RocketMQ 消息存储

    消息存储 主要的存储文件: 1.消息文件(commitLog) 2.消息消费队列文件(consumeQueue) 3.Hash索引文件(IndexFile) 4.检测点文件(checkpoint) 5 ...

  8. JAVA 并发:CLH 锁 与 AbstractQueuedSynchronizer

    首先向Doug Lea致敬. CLH 以下是CLH锁的一个简单实现: class SimpleCLHLock { /** * initialized with a dummy node */ priv ...

  9. CSS计数器(序列数字字符自动递增)详解———张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4303 一.挖坟不可耻 ...

  10. jquery中 苹果手机对on触发的点击事件无效果

    在被点击的元素上加上样式  cursor:pointer;  苹果手机就可以触发事件了