解决问题,链表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;
}
原文地址:https://www.cnblogs.com/kobe961231/p/5513564.html