C++ ONLY -- PRACTICE ASSIGNMENT
For our practice assignment we have to turn in 2 files - Driver.cpp and StringQueue.h
Driver.cpp is provided for us, but if there are any changes needed to be made to that file please let me know.
Based on the instructions below, what should the code look like for StringQueue.h ?
PROVIDED Driver.cpp FILE:
#include <iostream>
#include "StringQueue.h"
using namespace std;
int main()
{
const int MAX_VALUES = 10;
//create the queue
StringQueue myQueue;
string candy;
cout << "\n\nMomma is going to buy us some candy!\n";
cout << "She may not let us buy all of it so lets make a queue \n";
cout << "and add our top 10 candy choices in the order from most favorite to \n";
cout << "least favorite so she will buy our most favorite first!!\n\n";
// User enters their fav candy
for (int x = 0; x < 10; x++)
{
cout << "CANDY " << x+1 << ": ";
getline(cin, candy);
myQueue.push_back(candy);
}
// Mom buys the candy
cout << "\nYay! Momma is home from the Piggly Wiggly! Here is the candy she bought:\n";
while (!myQueue.isEmpty())
{
candy = myQueue.pop_front();
cout << candy << endl;
}
cout << "\nYESSSSSS! She bought it all!\n\n";
return 0;
}
#include <string>
using namespace std;
struct QueueNode
{
string value;
QueueNode *next;
};
class StringQueue
{
QueueNode *front,*rear;
int numItems;
public:
StringQueue()
{
rear=NULL;
front=NULL;
numItems = 0;
}
bool isEmpty();
void push_back(string s);
string pop_front();
};
bool StringQueue::isEmpty(){
if (numItems > 0 )
return false;
else
return true;
}
void StringQueue::push_back(string s)
{
QueueNode *temp;
temp = new QueueNode;
temp->next = NULL;
temp->value = s;
if(rear == NULL)
{
rear = temp;
front = temp;
}
else
{
rear->next=temp;
rear=temp;
}
numItems++;
//cout<<"Itempushed, no of items = "<<numItems<<"\n";
}
string StringQueue::pop_front()
{
if(front!=NULL)
{
QueueNode *temp = front;
string s = front->value;
front = front->next;
delete temp;
if(front==NULL)
rear=NULL;
numItems--;
//cout<<"Itempopped, no of items = "<<numItems<<"\n";
return s;
}
else
cout<<"Queue Empty..";
}
Above is the fully working code for StringQueue.h , just create a file with the same name and copy-paste the above code and you are done.
Below are the screenshots for the sample output and the code indentation.
Sample output
Please let me know if you have any questions/queries. Thanks.
Get Answers For Free
Most questions answered within 1 hours.