In: Computer Science
/* 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;
}
#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;
}