Need Help Please !! Write a C++ program to do the following: Define a structure
ID: 3774468 • Letter: N
Question
Need Help Please !!
Write a C++ program to do the following: Define a structure StringListElement with two members, a std::string named word and a StringListElement pointer named link. Read the a-words file one word at a time, assigning each word to the word member of a new instance of StringListElement. Keep a pointer to the head of the list and to the current tail of the list. Add a new element to the end of the list with the following steps: allocate a new instance, read the next word into the word member of the new instance, set the link member of the tail to the new instance, set the new instance link to 0 to indicate it is the new end of the list, update the tail pointer When the file is completely read, allocate an array of std::strings with size equal to the number of words read. Then starting at the beginning of the array, copy each word in the list to the array in order. Write a binary search function for strings. Use the binary search function to determine whether the strings "aa", "aardvark", "america", "anno", "apniate", and "azyms" are in the file of a-words. Display an output line for each search and whether it succeeded or failed. Free all the allocated storage
I already did the above program and it is as follows:-
static int assign_binarySearch()
{
std::string path = "path-to-your-file";
std::string keys[] = {"aa", "aardvark", "america", "anno", "apniate", "azyms"};
struct StringListElement {
std::string word;
StringListElement *link;
};
StringListElement *head, *next, *tail;
int keyCount = (sizeof keys) / (sizeof keys[0]);
int wordCount;
std::string *wordArray;
std::ifstream file;
std::string word;
int ix;
file.open(path);
if (!file)
{
std::cout << "unable to open file: " << path << std::endl;
return -1;
}
if (!getline(file, word))
{
std::cout << "input file " << path << " is empty." << std::endl;
return 0;
}
head = new StringListElement;
head->word = word;
head->link = 0;
tail = head;
wordCount = 1;
while (getline(file, word))
{
++wordCount;
next = new StringListElement;
next->word = word;
next->link = 0;
tail->link = next;
tail = next;
}
file.close();
wordArray = new std::string[wordCount];
for (ix = 0, next = head; next != 0; ++ix, next = next->link)
{
wordArray[ix] = next->word;
}
for (ix = 0; ix < keyCount; ++ix)
{
std::cout << "key " << keys[ix];
std::cout << ((0 <= str_bsearch(keys[ix], wordArray, wordCount)) ? "" : " NOT");
std::cout << " found." << std::endl;
}
while (head != 0)
{
next = head->link;
delete head;
head = next;
}
delete[] wordArray;
return 0;
_______________________________________________________________________________________________________
C++ program---But now I need to Modify the program of the previous assignment (That I pasted above) to use a class instead of a structure. Define the class as follows:
Add code for each of the methods outside the class using the class name and the scope resolution operator
Explanation / Answer
static int assign_binarySearch()
{
std::string path = "path-to-your-file";
std::string keys[] = {"aa", "aardvark", "america", "anno", "apniate", "azyms"};
struct StringListElement {
std::string word;
StringListElement *link;
};
{
StringListElement *head, *next, *tail;
int keyCount = (sizeof keys) / (sizeof keys[0]);
int wordCount;
std::string *wordArray;
std::ifstream file;
std::string word;
int ix;
file.open(path);
if (!file)
{
std::cout << "unable to open file: " << path << std::endl;
return -1;
}
if (!getline(file, word))
{
std::cout << "input file " << path << " is empty." << std::endl;
return 0;
}
head = new StringListElement;
head->word = word;
head->link = 0;
tail = head;
wordCount = 1;
while (getline(file, word))
{
++wordCount;
next = new StringListElement;
next->word = word;
next->link = 0;
tail->link = next;
tail = next;
}
file.close();
wordArray = new std::string[wordCount];
for (ix = 0, next = head; next != 0; ++ix, next = next->link)
{
wordArray[ix] = next->word;
}
for (ix = 0; ix < keyCount; ++ix)
{
std::cout << "key " << keys[ix];
std::cout << ((0 <= str_bsearch(keys[ix], wordArray, wordCount)) ? "" : " NOT");
std::cout << " found." << std::endl;
}
while (head != 0)
{
next = head->link;
delete head;
head = next;
}
delete[] wordArray;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.