DS Malik の書籍「C++ を使用したデータ構造」を読んでいます。次の検索機能について少し戸惑っています(リンクされたリストの場合)
マリクによれば、「検索項目がリストのi番目の項目である場合、while ループは i 回実行されます。以下は本からの正確なコードです (コメントなし)。
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;
}
アイテムが見つかったら、このループは本当に停止しますか?
while (current != NULL && !found)
私の本能は、これらの && 演算子を使用して続行すると言っていますが、間違っている可能性があります。本のタイプミスですか、それとも何か抜けていますか?
もう 1 つの問題は、コンパイラが不平を言う次の行です。
current = first; //error 'first' was not declared in this scope
それを修正するために、私はそれを
current = searchItem.first;
コンパイラはもう文句を言いませんが、親クラスから適切な保護されたメンバーにアクセスしていますか? nodeType<Type> *first
( unorderedLinkList は、保護されたメンバーを持つlinkedListType親クラスから継承します)
編集:より多くのコード: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
};
編集:派生クラス
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
}
編集: VS Express は私のコードをコンパイルしますが、このサイトはコンパイルしません。助けてください?T_T http://ideone.com/SN2R99