解决问题,链表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 ...
随机推荐
- JAVA练手--异常
1. 基本的 public static void main(String[] args) { //1. try catch基本用法 { try{ int[] intA = new int[2]; i ...
- 在ViewDidLoad中往导航栈推ViewController报错
Unbalanced calls to begin/end appearance transitions for <YZPMainViewController: 0x7fa04b4970f0& ...
- LightningChart 客户案例分享-DCC 环境工程
DCC Dynamics 致力于为建筑管控行业生产OEM的监控和管理产品.公司的旗舰产品“环境物流系统Environmental Logistics System” 用于大型建筑,校园及研究所设施,有 ...
- JAVA注释方式
1.单行(single-line)注释 //…… 2.块(block)注释 /*……*/ 3.文档注释 /**……*/
- 最新sublime Text3 注册激活码
sublime build 3103注册码 Enter License -- BEGIN LICENSE --Ryan ClarkSingle User LicenseEA7E-8124792158A ...
- poj Strange Way to Express Integers 中国剩余定理
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8193 ...
- Redis-SDS
Redis 的简单动态字符串 (simple dynamic string,SDS) SDS的结构: struct sdshdr { int len; //保存的字符串长度. int free; ...
- delphi之读写文件的三种方式
一.Tstrings.Tstringlist procedure TForm1.Button2Click(Sender: TObject); var strlist: TStringList; pat ...
- Java基础小结
JavaSE基础 本文为作者在学习和笔试题中遇到的小知识点总结,做以总结和备用. jdk的安装和配置环境变量 (1)以win10为例,右键此电脑,选择属性,进去系统设置,选择高级系统设置,进入环境变量 ...
- [COCI2006-2007#1] Bond
状压DP \(dp[i]\)表示当前选人状态为\(i\)且选择了前\(i.count()\)个物品时最大的概率 #include"cstdio" #include"cst ...