Write a C program to do the following The attached file of E

Write a C++ program to do the following:

The attached file of English words beginning with a is all lower case in sorted order. 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.

a-words file :- I couldn\'t able to upload the file, so pasted the words below

aa
aah
aahed
aahing
aahs
aal
aalii
aaliis
aals
aardvark
aardvarks
aardwolf
aardwolves
aargh
aarrgh
aarrghh
aas
aasvogel
aasvogels
ab
aba
abac
abaca
abacas
abaci
aback
abacs
abacterial
abactinal
abactinally
abactor
canths
acanthus
acanthuses
acapnia
acapnias
acari
acarian
acariases
acariasis
acaricidal
acaricide
acaricides
acarid
acaridan
acaridans
acaridean
acarideans
acaridian
acaridians
acaridomatia
acaridomatium
acarids
acarine
acarines
acarodomatia
alphabetized
alphabetizer
alphabetizers
alphabetizes
alphabetizing
alphabets
alphameric
alphamerical
alphamerically
alphametic
alphametics
alphanumeric
alphanumerical
alphanumerically
alphanumerics
alphas
alphasort
alphasorted
alphasorting
alphasorts
anno
annona
annonas
annotate
annotated
annotates
annotating
annotation
annotations
annotative
annotator
annotators
announce
announced
announcement
announcements
announcer
announcers
announces
announcing
annoy
annoyance
annoyances
annoyed
annoyer
annoyers
annoying
annoyingly
annoys
anns
annual
annualise
annualised
azurite
azurites
azurn
azury
azygies
azygos
azygoses
azygous
azygy
azym
azyme
azymes
azymite
azymites
azymous
azyms

__Thank you so much for helping__

Solution

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;

struct StringListElement
{
   string str;
   struct StringListElement *next;
};
int binarySearch(string names[], int size, string value);

int main()
{
   ifstream inFile;
   struct StringListElement *head=NULL,*teail=NULL,*cur, *newptr;
   int cnt = 0; //no of words in file
   string str1;

   inFile.open(\"strList.txt\");

   if(inFile.fail())
   {
       cout<<\"Not able to open input file\"<<endl;
       exit(1);
   }

   while(!inFile.eof())
   {
       inFile>>str1;
       if( head == NULL )
       {
           newptr = new StringListElement;
           newptr->str = str1;
           newptr->next=NULL;
           head = newptr;
           cur = head;
       }
      
       else
       {
           while( cur->next!= NULL)
           cur = cur ->next;
           newptr = new StringListElement;
           newptr->str = str1;
           newptr->next=NULL;
           cur ->next=newptr;
           cur = newptr ;
           cnt++;
       }
   }

   std::vector<std::string> array_str(cnt+1); //we can use string of vector instead string of arrays.
   std::string *arraystr1; //aray of strings
  
   arraystr1 = new std::string[cnt+1];
   cur = head;
   int i = 0;
   while(cur!=NULL)
   {

       array_str[i]= cur->str;
       arraystr1[i++]= cur->str;
       cur = cur->next;
   }
   cout<<\"Array Elements are : \";
   for( i = 0 ; i< cnt;i++)
   {
       //cout<<array_str[i]<<endl;
       cout<<arraystr1[i]<<endl;
   }

   int ch,ret;
   //ch=getchar();
   while( 1)
   {
       cout<<\"Enter any number to search the names or zero to quit\"<<endl;
       cin.ignore();
       cin>>ch;
      
       if ( ch == 0)
           break;

       cout<<\"Enter the string to search \";
       cin>>str1;
       if( (ret= binarySearch(arraystr1,cnt,str1))>=0)
       {
           cout<<\"String \" <<str1<<\" is found in the array at \"<<ret+1<< \" position\"<<endl;
       }
       else
           cout<<\"String \" <<str1<<\" is not found in the array\"<<endl;
   }
  
}


int binarySearch(string names[], int size, string value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag

while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (names[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (names[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}

Write a C++ program to do the following: The attached file of English words beginning with a is all lower case in sorted order. Define a structure StringListEle
Write a C++ program to do the following: The attached file of English words beginning with a is all lower case in sorted order. Define a structure StringListEle
Write a C++ program to do the following: The attached file of English words beginning with a is all lower case in sorted order. Define a structure StringListEle
Write a C++ program to do the following: The attached file of English words beginning with a is all lower case in sorted order. Define a structure StringListEle
Write a C++ program to do the following: The attached file of English words beginning with a is all lower case in sorted order. Define a structure StringListEle

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site