Search code examples
c++linked-listmember-functionsblacklistword-cloud

Comparing One Link List to Another Blacklist versus Word Frequency List C++


I have created a program that will read a text file and put the words as strings into a linked list, along with their frequency count throughout the text file. It only prints one occurrence of each word with the total times it appeared.

My program also loads a blacklist, in which it is supposed to compare the blacklist linked list to the word cloud (or word frequency) linked list, and then remove the blacklisted words from the word frequency list.

I have tried doing this several ways. The following is my 3rd version. What I am wanting to do is add a Boolean value to each node, and when one node is equal to a word in the blacklist, the Boolean value will be true. However, I am not getting it to print right with the following code. I have searched, and I can't seem to find the correct syntax to add a Boolean value to a node in linked list.

EDIT #3:

void wordCloud::compareWith(wordCloud& wordList, wordCloud& badList){
wordNode *wordListTemp, *blacklistTemp, *temp = NULL;
unsigned int counter = 0;

for (blacklistTemp = badList.head; blacklistTemp; blacklistTemp = blacklistTemp->next){
    cout << blacklistTemp->myWord << "\n";
    for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){

        if (wordListTemp->myWord != blacklistTemp->myWord){

            wordListTemp->blacklist = false;
            if (wordListTemp->blacklist = false){
                cout << wordListTemp->myWord << " <"
                    << wordListTemp->freq_count << ">\n";
            }
        }
        else if (wordListTemp->myWord == blacklistTemp->myWord){
            cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";
            wordListTemp->blacklist = true;
            if (wordListTemp->blacklist = true)
                cout << wordListTemp->myWord << "\n";
        } 
    }
    //counter++;
    cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";
}

system("pause");
}

This is not complete, but it is as far as I have gotten. The problem is it only prints the true if, and will not print any false if. Even if I switch the values, it will still only print the true if's. So I am assuming that I am going about this wrong. What would be the correct way to "flag" a node true and "flag" a node false? All the cout's are for debugging purposes. I will remove or comment those out later.


Solution

  • Finally!!

    With a lot of old fashion debugging and cout statements, I finally got what I wanted. I know this might have been easy for some, but with not being very familiar with linked lists, this was quite the process for me.

    Before I was trying to delete words that were seen in the blacklist linked list out of the wordList linked list. I decided later to just try to add a boolean value of true to the nodes in wordList, and then adjust my print function to not print nodes with the value of true. I also had to tweak a few things in insertWord(), and my freqSort() functions, but all that really consisted of was adding a pointer to the boolean value when a new node was being created.

    My member function is void wordCloud::compareWith(wordCloud& wordList, wordCloud& badList), and is part of my wordCloud class. Here is the following definition:

    void wordCloud::compareWith(const wordCloud& wordList, const wordCloud& badList){
    wordNode *wordListTemp, *blacklistTemp;
    unsigned int counter = 0;
    
    //loop that advances wordListTemp
    for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){
        blacklistTemp = badList.head;
    
        //loop advances blacklistTemp - compares links in wordList to badList(blacklist)
        //and sets the node to true if myWord equals any word in the blacklist
        while (blacklistTemp){          
            if (wordListTemp->myWord == blacklistTemp->myWord){
                wordListTemp->blacklist = true; 
                counter++;
            }
            blacklistTemp = blacklistTemp->next;
        }
    
        //for debugging
        //cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";     
    }
    
    /*********************  All for debugging  ***************************************
    cout << "True:\n\n";
    wordListTemp = wordList.head;       //reset wordListTemp to head    
    
    while (wordListTemp){               //print blacklisted words from wordList
        if (wordListTemp->blacklist == true){
            cout << wordListTemp->myWord << " <"
                << wordListTemp->freq_count << ">\n";
        }
        wordListTemp = wordListTemp->next;
    }
    //prints total words blacklisted
    cout << "There are " << counter << " blacklisted words.";                   
    
    cout << "\n\nFalse:\n\n";
    wordListTemp = wordList.head;       //reset wordListTemp to head    
    counter = 0;
    
    while (wordListTemp){               //print non-blacklisted words from wordList
        if (wordListTemp->blacklist == false){
            cout << wordListTemp->myWord << " <"
                << wordListTemp->freq_count << ">\n";
            counter++;
        }
        wordListTemp = wordListTemp->next;
    }
    //prints total words not blacklisted
    cout << "There are " << counter << " words that are not blacklisted.\n";
    
    system("pause");    
    ********************  End debugging *******************************************/    
    }
    

    So basically this is a comparison function that flags nodes that are found in another list. Works well and tested with all other options.