反向循环链表功能出错! ! [英] Reverse circular linked list fucntion goes wrong ! !

查看:83
本文介绍了反向循环链表功能出错! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为循环种类链表的还原编写函数!!我已经阅读了一个东西,我理解使用3个指针,我发现它特别困难也不工作,所以任何替代只使用最后或我的意思是不使用3指针仍然可以反转!!!



我尝试过:



i am trying to write function for the reversion of the circular kind linked list !! i have read a thing and i understand using 3 pointers and i am finding it particularly difficult also NOT working , so any alternative with only using "last" or i mean not using 3 pointers and still reversing !!!

What I have tried:

struct node
{
	int data;
	struct node *next;
}*last = NULL, *p, *q, *rear = NULL, *x;



//反向函数,难以理解但尚未给出结果


//Reverse Function , difficult to understand and yet not giving results

void circular_llist::reverse()
{
	x = last->next;
	p = last;
	last->next = NULL;
	while (x)
	{
		q = x->next;
		x->next = p;
		last = x;
		p = x;
		x = q;
	}

}



// Main


//Main

int main()
{
	circular_llist cl;
      cl.reverse();
}

推荐答案

使用调试器。这就是它的一部分。

在函数的开头放置一个断点,然后仔细查看正在发生的事情。

有什么可以帮助你理解的是,如果你首先在纸上绘制列表的样子 - 设置一个简单的4个节点 - 然后用它在你完成时的样子标记它。当您在调试器中运行代码时更改它,标记您的绘图以显示新状态。一旦你知道发生了什么,以及它出错的原因应该是相当明显的。
Use the debugger. That's part of what it is there for.
Put a breakpoint at the start of the function, and step through look at closely at what is going on.
What may help you understand is if you draw out on paper first what the list looks like - set up a simple one with say 4 nodes - then mark it with what it should look like when you are finished. As it gets changed while you run your code in the debugger, mark up your drawing to show the new state. If should be reasonably obvious once you get into it what is happening, and why it's going wrong.


假设你有 p,q,r ,指向三个连续的节点,即

Suppose you have p,q,r, pointing to three consecutive nodes, namely
q = p->next;
r = q->next;



反向更新步骤是


the reverse-and-update step is then

q->next = p; // reverse
// update
p = q; 
q = r;
r = r->next;





止损条件为 q 等于列表的头部。


引用:

//反向功能,难度理解但不给出结果

//Reverse Function , difficult to understand and yet not giving results

正常,你在搞乱指针直到列表的末尾,问题列表是循环列表,没有结束!



正如你多次被告知的那样,使用调试器看你的代码在做什么。

你想要建议但不要遵循它们。看起来你需要最少的努力才能获得完整的爆炸解决方案。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。



Debugger - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到bug,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。



建议:拿一张纸并尝试手工模拟你的算法,你的程序应该使用相同的程序。

Normal, you are messing with pointers until the end of the list, problem the list is circular list, there no end!

As uou have been told multiple times, use the debugger and see what your code is doing.
You want advice but don't follow them. Looks like you want full blowup solutions with minimum effort.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to simulate your algorithm by hand, your program should use the same procedure.


这篇关于反向循环链表功能出错! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆