解决问题,链表finish
从一个不懂链表,到反反复复改了不下50遍,提交该题页数更是突破了五页,从周三下午到周五中午的面向对象课前的20分钟,终于把这道题AC了,其实这题本来是原来C语言综合实验的一道题,但是本次在PAT上的审核却没有那么简单。
说下心路历程
- 其实这个链表在开始的十个小时的学习中对我来说就跟之前的CLASS一样,完完全全的新东西,不知道从何temp->next,也不知道题意,让我反转什么。圈圈画画,终于写了出来,后来发现前面写的指针代码都比较复杂。可以简化。在之后的小时里都是在跟PTA的测试点作斗争,正如前面的随笔所言,只有两个AC,我就在想为什么有两个AC而其他的问题都是答案错误,于是我遍历了所有的代码,感觉虽然愚钝,但是符合逻辑,可执行才对,于是开始了各种数据的测试,从小数据,特殊数据,到大数据,乱七八糟的,打乱顺序的都试过,然而命运就像是和我开玩笑,测试的数据全对,但是过不了PAT的测试点。当时的我真的想把PAT的测试点挖出来问问,到底是什么原因。因此我上了C语言综合实验的测试网站,提交了自己的代码,结果竟然是AC了。这我就更疑惑了,为什们我能够通过那个测试,却过不了PAT,在沉默了跟打代码一样长的时间后,我终于发现了问题症结。PAT测试点会呈现段错误,而段错误在我这里会因为某种原因继续下去,因为我的段错误是一种非数组越界,或者是栈 队列溢出那种的问题,我的问题在于循环中加入了太多的循环和判断,我想这个问题原因可能是对于小数据,PAT会规定具体的循环次数,以及if的次数如果超过,就会段错误,但是会继续执行,答案输出了,即使对了,也会按照答案错误处理并张贴。这个就会让人误以为是代码的问题,而实际并不是。PAT果然严格。不过希望他能告诉我是段错误。
下面贴出代码和截图。

代码部分
#include<iostream>
#include<stdlib.h>
#include<string>
#include<iomanip>
#include<stdio.h>
using namespace std;
int main()
{
struct Date
{
int Adress;
int num;
int Next;
struct Date *next;
};
int all, ts, i = 0;
Date*head = (Date*)malloc(sizeof(Date));
Date*p1 = (Date*)malloc(sizeof(Date));
Date*p2 = (Date*)malloc(sizeof(Date));
scanf("%d %d %d", &head->Adress, &all,&ts);
while (i != all)
{
Date*p1 = (Date*)malloc(sizeof(Date));
scanf("%d %d %d", &p1->Adress, &p1->num, &p1->Next);
if (i == 0)
head->next = p1;
else
p2->next = p1;
p2 = p1;
i++;
}
p2->next = NULL;
Date*temp1 = (Date*)malloc(sizeof(Date));
Date*temp2 = (Date*)malloc(sizeof(Date));
Date*r = (Date*)malloc(sizeof(Date));
Date*p = (Date*)malloc(sizeof(Date));
temp1 = head;
temp2 = temp1->next;
while (1)
{
if (head->Adress == temp2->Adress)
{
p = temp1;
for (;;)
{
if (p->next == temp2)
{
p->next = temp2->next;
break;
}
else
{
p = p->next;
}
}
r = head->next;
head->next = temp2;
temp2->next = r;
break;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
temp2 = temp1->next;
while (temp1 != NULL)
{
while (temp2 != NULL)
{
if (temp2->Adress == temp1->Next&&temp2->Next != -1)
{
p = temp1;
for (;;)
{
if (p->next == temp2)
{
p->next = temp2->next;
break;
}
else
{
p = p->next;
}
}
r = temp1->next;
temp1->next = temp2;
temp2->next = r;
break;
}
else if (temp2->Adress == temp1->Next&&temp2->Next == -1)
{
p = temp1;
for (;;)
{
if (p->next == temp2)
{
p->next = temp2->next;
break;
}
else
{
p = p->next;
}
}
temp1->next = temp2;
temp2->next = NULL;
break;
}
else
{
temp2 = temp2->next;
continue;
}
}
if (temp1->next != NULL)
{
temp1 = temp1->next;
temp2 = temp1->next;
}
else
break;
}
int js = 0;
Date*check = (Date*)malloc(sizeof(Date));
check = head->next;
for (;;)
{
if (check->Next != -1)
{
check = check->next;
js++;
}
else if (check->Next == -1)
{
js++;
break;
}
}
all =js;
if (ts == 0)
ts = 1;
int j=all/ts;
if (ts > all || all == 1||j==0)
j = 0;
else
{
temp1 = head;
temp2 = temp1->next;
i = 0;
for (int l = 1; j > 0; l++, j--)
{
while (i != l*(ts - 1))
{
temp2 = temp2->next;
i++;
}
p1 = temp2;
r = temp2->next;
p = temp1;
while (p->next != temp2)
{
for (;;)
{
if (p->next == temp2)
{
temp2->next = p;
temp2 = temp2->next;
p = temp1;
break;
}
else
{
p = p->next;
}
}
}
temp2->next = r;
temp1->next = p1;
while (temp1 != temp2)
{
temp1 = temp1->next;
}
if (temp1 == NULL)
break;
else
{
temp2 = temp1->next;
}
}
}
temp1 = head->next;
temp2 = temp1->next;
head->Adress = head->next->Adress;
for (i = 0; i < all-1 ; i++)
{
temp1->Next = temp2->Adress;
temp1 = temp1->next;
temp2 = temp1->next;
}
if(temp1->next==NULL)
temp1->Next = -1;
Date*out = (Date*)malloc(sizeof(Date));
out = head->next;
while (out != NULL)
{
if (out->Next != -1)
{
cout << setfill('0') << setw(5) << out->Adress << " " << out->num << " " << setfill('0') << setw(5) << out->Next << endl;
out = out->next;
}
else
{
cout << setfill('0') << setw(5) << out->Adress << " " << out->num << " " << out->Next << endl;
out = out->next;
}
}
return 0;
}
解决问题,链表finish的更多相关文章
- 防御性编程习惯:求出链表中倒数第 m 个结点的值及其思想的总结
防御性编程习惯 程序员在编写代码的时候,预料有可能出现问题的地方或者点,然后为这些隐患提前制定预防方案或者措施,比如数据库发生异常之后的回滚,打开某些资源之前,判断图片是否存在,网络断开之后的重连次数 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
- 剑指Offer面试题:4.从尾到头打印链表
一.题目:从尾到头打印链表 题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. 到解决这个问题肯定要遍历链表.遍历的顺序是从头到尾的顺序,可输出的顺序却是从尾到头.也就是说第一个遍历到的结 ...
- 剑指Offer面试题:12.在O(1)时间删除链表结点
一.题目:在O(1)时间删除链表结点 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 原文采用的是C/C++,这里采用C#,节点定义如下: public class ...
- 剑指Offer面试题:14.链表的倒数第k个节点
PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...
- delphi.数据结构.链表
链表作为一种基础的数据结构,用途甚广,估计大家都用过.链表有几种,常用的是:单链表及双链表,还有N链表,本文着重单/双链表,至于N链表...不经常用,没法说出一二三来. 在D里面,可能会用Contnr ...
- 数据结构之链表C语言实现以及使用场景分析
牢骚:本篇博客两个星期前已经存为草稿,鉴于发生一些糟糕的事情,今天才基本完成.本人6月份应届毕业生一枚,毕业后当天来到帝都,之后也非常顺利,面试了俩家公司都成功了.一家做C++方面电商ERP,一家做w ...
随机推荐
- 师傅领进门之6步教你跑通一个AI程序!
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云计算基础发表于云+社区专栏 源码下载地址请点击原文查看. 初学机器学习,写篇文章mark一下,希望能为将入坑者解点惑.本文介绍一些机 ...
- 图像RGB2YUV与YUV2RGB格式互转介绍
1 YUV格式与RGB格式说明 由于不同国家的电视信号系统支持的图像格式不同,有YUV格式成像,也有RGB格式成像,因此为了保证兼容性,需要进行RGB与YUV格式的互转. 另外YUV格式具有亮度信息和 ...
- Android组件--意图(Intent)
1. 隐示调用和显示调用 参考资料:http://blog.csdn.net/harvic880925/article/details/38399723 1.概念 1). 显式意图: 能从intent ...
- Paypal如何实现循环扣款(订阅)?
起因 业务需求要集成Paypal,实现循环扣款功能,然而百度和GOOGLE了一圈,除官网外,没找到相关开发教程,只好在Paypal上看,花了两天后集成成功,这里对如何使用Paypal的支付接口做下总结 ...
- android LinearLayout
Android的布局方式共有6种,分别是LinearLayout(线性布局).TableLayout(表格布局).FrameLayout(帧布局).RelativeLayout(相对布局).GridL ...
- [C#]浅谈协变与逆变
看过几篇说协变与逆变的博客,虽然都是正确无误的,但是感觉都没有说得清晰明了,没有切中要害.那么我也试着从我的理解角度来谈一谈协变与逆变吧. 什么是协变与逆变 MSDN的解释:https://msdn. ...
- 从Linux系统安装到Web应用启动教程
概述 本文讲述web应用服务器安装配置教程,其中包括:Linux系统安装,Mysql数据库安装配置,Redis安装配置,Tomcat安装配置,MongoDB安装配置,Linux JDK安装使用,Ngi ...
- K:求取数组中最大连续子序列和的四个算法
相关介绍: 求取数组中最大连续子序列和问题,是一个较为"古老"的一个问题.该问题的描述为,给定一个整型数组(当然浮点型也是可以的啦),求取其下标连续的子序列,且其和为该数组的所有 ...
- PHP 经典算法
<? //-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) function bin_s ...
- jquery replace方法去空格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...