Question

In: Computer Science

/* WordList source file * * *   This file will contain the function definitions you will...

/* WordList source file
*
*
*   This file will contain the function definitions you will implement.
*   The function signitures may NOT be changed. You may create your own
*   helper functions and include them in this file.
*
*   In addition to the specific instructions for each function, no function
*   should cause any memory leaks or alias m_list in any way that would result
*   in undefined behavior.
*
*   Topics: Multilevel Pointers, Dynamic Allocation, Classes
*
*/

private:
#endif

   unsigned int m_count;   // Number of words currently in list
   unsigned int m_max;       // The total size of the list.
   char** m_list;   // The list storing the words

};

/* Function: Wordlist Constructor
*/
WordList::WordList(const int max_words) {

   m_count = 0;
   if (max_words > 0) {
       m_max = max_words;
       m_list = new char* [max_words];
   }
}

/* Function: Wordlist Copy Constructor
*/
WordList::WordList(const WordList& other) {

   m_count = other.m_count;
   m_max = other.m_max;
   m_list = new char* [m_max];
   for (int i = 0; i < m_max; i++) {
       m_list[i] = other.m_list[i];
   }

}
/* Function: Wordlist Destructor
*/
WordList::~WordList() {

   delete []m_list;

}

/* Function: printList
*/
int   WordList::print() const {   // TODO:
   return -1;

}

/* Function: at
*/
char* WordList::at(const int index) const {   // TODO:
   return nullptr;

}
/* Function: count
*/
int   WordList::count() const { // TODO:
   return -1;

}
/* Function: add
*/
int   WordList::add(const char word[]) { // TODO
   return -2;

}
/* Funtion: remove
*/
int   WordList::remove(const char word[]) { // TODO:
   return -1;
}

/* Funtion: append
*/
int   WordList::append(const WordList* other) { // TODO:
   return -2;

}
/* Funtion: search
*/
int WordList::search(const char word[]) const {   // TODO:
   return -1;

}
/* Funtion: sort
*/
int   WordList::sort() {   // TODO:
return -1;

}
/* Funtion: Assignment Operator
*/
WordList& WordList::operator=(const WordList& other) { // TODO:
   return *this;
}

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;
const int E =  0.01;
const int MAX_ITER = 100;


class Wordlist {
private:
        unsigned int m_count, m_max;
        char ** m_list;

}


WordList::WordList(const int max_words) {
        m_count = 0;
        if (max_words > 0) {
                m_max = max_words;
                m_list = new char*[max_words];
        }
}


Wordlist::Wordlist(const Wordlist& other) {
        m_count = other.m_count;
        m_max = other.m_max;
        m_list = other.m_list;
        for (int i = 0; i < m_max; i++)
                m_list[i] = other.m_list[i];
}


WordList::~WordList() {
        delete []m_list;
}

int Wordlist::print() const {
        for (int i = 0; i < m_max; i++) {
                cout << m_list[i] << " ";
        }
        return -1;
}


char *Wordlist::at(const int index) const {
        return m_list[index];
}

int Wordlist::count()const {
        // int cnt = 0;
        return m_count;
}


int Wordlist::add(const char word[]) {
        char** tmp = new char*[m_count];

        for (int i = 0; i < m_count; i++)
        {
                tmp[i] = m_list[i];
        }

        m_count++;

        delete[] m_list;
        m_list = tmp;

        m_list[m_count - 1] = word;

}

int Wordlist::remove(const char word[]) {
        for (int i = 0; i < m_count; i++) {
                if (m_list[i] == word) {
                        for (int j = i + 1; j < m_count; j++) {
                                m_list[j - 1] = m_list[j];
                        }
                        return 1;
                }
        }
        return -1;
}

int Wordlist::search(const char word[]) {
        for (int i = 0; i < m_count; i++) {
                if (m_list[i] == word) {
                        return i;
                }
        }
        return -1;
}

int WordList::sort() {
        bool exchanges;
        do {
                exchanges = false; // assume no exchanges
                for (int i = 0; i < m_count - 1; i++) {
                        if (m_list[i] > m_list[i + 1]) {
                                double temp = m_list[i];
                                m_list[i] = m_list[i + 1];
                                m_list[i + 1] = temp;
                                exchanges = true; // after exchange, must look again
                        }
                }
        } while (exchanges);

        return 1;
}




Wordlist& Wordlist::operator=(const WordList& other) {
        for (int i = 0; i < m_count; i++) {
                m_list[i] = other[i];
        }
        return *this;
}

Related Solutions

C++ If you tried to compile a source file that doesn't contain a main() function, would...
C++ If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer. Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.
"You will need to compile each Java source file (provided below) separately, in its own file...
"You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory." My class is asking me to do this and i am not understanding how to do it. I use JGRASP for all of my coding and i can't find a tutorial on how to do it. I just need a step by step tutorial...
You are given a source file in your work area for this assignment. It consists of...
You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to reverse an integer. You can...
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the requirements for trap.cpp: Write a main program in a file called trap.cpp that solves the following scenario, using your Grid class: Giant Mole People have risen from their underground lairs and are taking over the world. You have been taken prisoner and placed in an underground jail cell. Since the Mole People are blind and don't know how to build doors, your cell has...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
For this assignment, you will write a program that consists of four function definitions, including one...
For this assignment, you will write a program that consists of four function definitions, including one main() function. The main function gets inputs from the user, and the other functions show output to the user. When the program is executed, it asks the user to enter a small amount of information, and then shows output based on the user's info. Specification: Here are some specific requirements for this program: The program should consist of four function definitions, written one after...
Write this program in C++ You are given a source file in your work area for...
Write this program in C++ You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to...
Write a function file that accepts each of the four vectors as inputs. The function file...
Write a function file that accepts each of the four vectors as inputs. The function file should plot the data on different graphs in the same window. You need to add axis labels and graph titles. time- 0 5 10 15 20 25 30 A1- 0 7 11 19 15 14 7 A2- 0 10 15 21 16 11 13 A3- 0 9 13 17 22 25 21
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT