代码停止工作?双链表 [英] Code stopped working ? double linked list
问题描述
当我尝试实现insertafter函数时,我收到错误停止工作。 。
帮我解决错误!!
我尝试了什么:
i am getting error "Stopped Working" when i am trying to implemet insertafter function. .
help me to solve the error !!
What I have tried:
void DLL::insert(int value)
{
if (head == NULL)
{
head = new Node;
head->next = NULL;
head->prev = NULL;
head->data = value;
last = head;
}
else
{
Node *temp = new Node;
temp->data = value;
temp->next = NULL;
temp->prev = last;
last->next = temp;
last = temp;
}
}
//显示功能和插入功能
//Display Function and insertafter function
void DLL::insertAfter(int v1, int v2)
{
Node *present = new Node;
while (present != NULL)
{
if (present->data == v1)
{
Node *temp = new Node;
temp->data = v2;
temp->next = present->next;
temp->prev = present;
present->next = temp;
present = temp;
break;
}
else
{
present = present->next;
}
}
}
void DLL::display()
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
}
//主要功能
//Main Function
void main()
{
int v1, v2;
DLL myList;
myList.insert(1);
myList.insert(2);
myList.insert(4);
myList.insert(5);
myList.insert(6);
myList.display();
cout << "\n";
cout << "PLEase enter the no Before that you want to enter a number." << endl;
cin >> v2;
cout << "Now enter new no." << endl;
cin >> v1;
myList.insertAfter(v1, v2);
myList.display();
}
推荐答案
这看起来很可疑:
This looks suspicious:
void DLL::insertAfter(int v1, int v2)
{
Node *present = new Node;
while (present != NULL)
{
您正在创建一个新的(单元化的)节点
对象。
应该可能是:
You are creating a new (unitialised) Node
object.
It should be probably:
void DLL::insertAfter(int v1, int v2)
{
Node *present = head;
while (present != NULL)
{
一旦你修复了你将遇到其他问题因为 insertAfter
没有调整下一个列表项的 prev
成员(present-> next-> prev = temp)并且在附加项目时不会更新 last
(项目包含 data == v1
实际上是列表中的最后一项。)
最后在 v1 $ c之间进行换位请求输入并在
insertAfter
中处理它之间的$ c>和 v2
(插入位置存储在 v2
这是插入时的数据值。
[/ EDIT]
Once you have fixed that you will run into other problems because insertAfter
does not adjust the prev
member of the next list item (present->next->prev = temp) and does not update last
when the item is appended (the item with data == v1
is actually the last item in the list).
Finally there is a transposition between v1
and v2
between asking for the input and handling it in insertAfter
(the insert position is stored into v2
which is the data value upon insertion).
[/EDIT]
看看你的insertAfter
方法:
Have a look at yourinsertAfter
method:
Node *present = new Node;
while (present != NULL)
{
if (present->data == v1)
{
//..
}
else
{
present = present->next; //<--- troubles here
}
//..
考虑新创建的现在
节点
。不幸的是你没有向我们展示 Node
构造函数,无论如何我们可以放心地假设它的 next
字段指向垃圾,及其数据
字段非常不可能等于 v1
。我看到了地平线上的麻烦。
Consider the freshly created present
Node
. Unfortunately you didn't show us the Node
constructor, anyway we could assume with some confidence its next
field pointing to garbage, and its data
field very unlikely equal to v1
. I see troubles on the horizon.
void DLL::insertAfter(int v1, int v2)
{
Node *present = new Node;
while (present != NULL)
您的函数与一起使用present
,一个未初始化的节点,取消链接到列表。
你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。
Debugger - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
your function works with present
, an uninitialized node unlinked to the list.
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.
这篇关于代码停止工作?双链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!