Working through the book "Data structures using C++" of D.S. Malik. I'm a bit puzzled about the following search function, (for linked lists)
According to Malik, "if the search item is the i th item in the list, the while loop executes i times. The following is the exact code from the book(without comments).
template <class Type>
bool unorderedLinkList<Type>::search(const Type& searchItem) const
{
nodeType<Type> *current;
bool found = false;
current = first;
while (current != NULL && !found)
if (current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
Will this loop really stop once the item is found?
while (current != NULL && !found)
My instinct tells me it will keep going with those && operators, but I might be wrong. Was it just a typo in the book, or am I missing something?
Another problem is the following line that my compiler complains about.
current = first; //error 'first' was not declared in this scope
So to fix it, I replaced it with
current = searchItem.first;
Compiler doesn't complain anymore, but is it accessing the right protected member from the parent class? (unorderedLinkList inherits from linkedListType parent class, which has protected nodeType<Type> *first
member)
Edit: More code :D
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListType
{
public: //some removed for space
virtual bool search(const Type& searchItem) const = 0;
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
//function to make a copy of otherlist and assign to this list
};
Edit: The derived class
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
}
Edit: VS Express compiles my code, but this site wont. Help please? T_T http://ideone.com/SN2R99