The following is a method for a linked list written in c++. The method is a simp
ID: 669583 • Letter: T
Question
The following is a method for a linked list written in c++. The method is a simple search. It takes in a value, and returns true if the value is on the list, and false if it isn't. The function does not work 100% of the time. It will return false on an empty list, and it will return false when the value is not on the list, however, it will ONLY return true if the value passed in is the first value on the list. any values past the first will be returned as false even if they acutally are there. I think the issue lies in the list transversal.
bool LinkedList::search(int value) const //PROBLEM - only returns true if the value is the first on the list.
{
Node* temp =m_front;
temp=new Node();
if(m_front==nullptr)
{
return(false);
}
else
{
temp = m_front;
while(temp!=nullptr)
{
if(temp->getValue()==value)
{
return(true);
}
temp=temp->getNext();//This must be the problem, perhaps this is somehow making temp null, ending the loop.
}
return(false); //The placement of this return might also be the issue
}
}
Explanation / Answer
I do not see any problem with the logic you have written can you please provide the programming language in which you have written this code and the corresponding helper function with the help of which it is easier to code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.