我认为“搜索”中的问题功能? [英] I think problem in "search" function ?
问题描述
我在搜索功能中遇到问题,因为我成功显示并在列表中插入元素,
我想要将元素/值搜索到列表中并显示结果!!我不会发错误!
我尝试过:
i am encountering problem in the search function, because i successfully display and insert elements into the list ,
i want that to search the element/value into the list and display result !! i am getting dont send error !!
What I have tried:
void DLL::search(int v1)
{
Node *present = new Node;
while (present != NULL)
{
if (present->data == v1)
{
cout << "Number Found" << endl;
}
else
{
present = present->next;
}
}
}
这是主要功能
Here is the main function
void main()
{
int v1, v2,v3;
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 >> v1;
cout << "Now enter new no." << endl;
cin >> v2;
myList.insertAfter(v1, v2);
myList.display();
cout << "\n";
cout << "Enter Value To search in List: ";
cin >> v3;
myList.search(v3);
}
推荐答案
您将重复错误:
You are going to repeat your errors:
Node *present = new Node;
必须是
must be
Node *present = head;
找到该项目时缺少一条休息语句:
And there is a break statement missing when the item is found:
void DLL::search(int v1)
{
Node *present = head;
while (present != NULL)
{
if (present->data == v1)
{
cout << "Number Found" << endl;
break;
}
else
{
present = present->next;
}
}
}
[/ EDIT]
[/EDIT]
Node *present = new Node;
您没有在列表中搜索!
再一次:
你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。
Debugger - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
You are not searching in the list !
Once again:
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屋!