iterating through a linked list

I am creating a spell checker that uses a dictionary in a linked list. I also have a book fin another that I am reading word by word.

I want to create a loop that will check the first word in the book against all the words in the linked list(dictionary). If the word is in the dictionary I will stop and count the word the correct, then move on to the next word in the book and read it against all words in the linked list and so on. If I get all the way through the linked list, the word will be counted as incorrect and we will do the same as earlier, get a new word from the book and check it against all the words in the linked list.

How can I do this? I can't find a simple way to iterate through a linked list while checking it against another value.
Last edited on
Don't start a new topic about the same subject.

https://www.cplusplus.com/forum/beginner/280166/
Last edited on
Read the words in the dictionary file into a list (std::list), if you must use a linked list.
For each word read in from the book file, spell check with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <list>
#include <string>
#include <algorithm>

// return true if the word is in the dictionary (use a loop)
bool spell_check( const std::list<std::string>& dictionary, const std::string& word ) {

    // for each string in the dictionary
    for( const auto& str : dictionary )
        if( word == str ) return true ; // the word matched this string in the dictionary, return true

    return false ; // reached the end without finding the word

}

// alternative: return true if the word is in the dictionary (use an algorithm)
bool spell_check2( const std::list<std::string>& dictionary, const std::string& word ) {

    // use the algorithm std::find https://en.cppreference.com/w/cpp/algorithm/find
    return std::find( dictionary.begin(), dictionary.end(), word ) != dictionary.end() ;
}


Note: adjust case, strip punctuation etc. for the words.
Topic archived. No new replies allowed.