can you please write the code for this This assignment provides you with an oppo
ID: 3832649 • Letter: C
Question
can you please write the code for this
This assignment provides you with an opportunity to work with one of the data structures we discussed in the last month of the semester: binary search trees, heaps, priority queues, and hash tables. You will design a class to hold a list of words in one of these data structures. Your wordList class should implement all appropriate operations for the data structure you use. You should submit three files for this assignment: prog6_main.cpp: Source file containing your main function. WordList, h/WordList.cpp: Header/source files containing the WordList class definition and member function definitions. Submit all three files by uploading them to your Dropbox folder. Ensure your file names match the names specified above. Do not place these files in a subdirectory - place them directly in your shared folder. Failure to meet this specification will reduce your grade, as described in the program grading guidelines. Input/output: Your main program should implement a string-based command interface that responds to the following commands: add: Prompts the user to enter a word and stores that word in the list delete: Prompts the user to enter a word and removes that word from the list, if possible print: Prints the entire list, preferably in alphabetical order first: Prints only the first word (in alphabetical order) in the list find: Prompts the user to enter a word and tests to see if that word is in the list. printing an appropriate message whether it is found or not exit: End program and exitExplanation / Answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node
{
int priority;
string word;
struct node *link;
};
class Wordlist_Priority_Queue
{
private:
node *front;
public:
Wordlist_Priority_Queue()
{
front = NULL;
}
void add(string item, int priority)
{
node *tmp, *q;
tmp = new node;
tmp->word = item;
tmp->priority = priority;
if (front == NULL || priority < front->priority)
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while (q->link != NULL && q->link->priority <= priority)
q=q->link;
tmp->link = q->link;
q->link = tmp;
}
}
void delete()
{
node *tmp;
if(front == NULL)
cout<<"Queue Empty ";
else
{
tmp = front;
cout<<"Deleted item is: "<<tmp->word<<endl;
front = front->link;
free(tmp);
}
}
void print()
{
node *ptr;
ptr = front;
if (front == NULL)
cout<<"Queue is empty ";
else
{ cout<<"Queue is : ";
while(ptr != NULL)
{
cout<<ptr->priority<<" "<<ptr->word<<endl;
ptr = ptr->link;
}
}
}
void first()
{
if (front == NULL)
cout<<"Queue is empty ";
else
cout<<front->word;
}
void find( string data)
{
node *ptr;
if (front == NULL)
cout<<"Queue is empty ";
else
{
ptr = front;
while (ptr != NULL && ptr->word != data)
ptr = ptr->link;
if (ptr == NULL)
cout << "Word not found" << endl;
else
cout << "Word is present" << endl;
}
}
};
int main()
{
int choice, priority;
string item;
Wordlist_Priority_Queue wpq;
do
{
cout<<"1.Add ";
cout<<"2.Delete ";
cout<<"3.Print ";
cout<<"4.first ";
cout<<"5.find ";
cout<<"6.Exit ";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the item to be added in the queue : ";
cin>>item;
cout<<"Enter its priority : ";
cin>>priority;
wpq.add(item, priority);
break;
case 2:
wpq.delete();
break;
case 3:
wpq.print();
break;
case 5:
cout<<"Input the item to be found in the queue : ";
cin>>item;
wpq.find(item);
break;
default :
cout<<"Incorrect choice ";
}
}
while(choice != 6);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.