Question

C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files...

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 ?

  1. Create a class named StringQueue in a file named StringQueue.h.
  2. Create a QueueNode structure as a private member of the class. The node should be able to hold a string called value.   This should be like a ListNode structure that you have created before for linked lists.
  3. Create a front pointer as private attributes of the class. (this is like the head pointer for a linked list)
  4. Also create a rear pointer as a private attribute of the class. (this is like the tail pointer for a linked list)
  5. Also create an integer named numItems, which will keep track with the number of items in the queue.
  6. Create the following public member functions:
    1. A constructor, which will set front & rear to NULL and numItems to zero.
    2. An isEmpty function, which will return true if the queue is empty and false otherwise.
    3. A push_back function, which will push a string at the END of the queue (like appendNode function for linked list)
    4. A pop_front function, which will remove & return the string from the front of the queue.

  1. Test your queue class using the Driver.cpp file provided for you.

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;

}

Homework Answers

Answer #1
#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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
In this assignment, you’ll make an inventory system for a store’s items, including produce and books....
In this assignment, you’ll make an inventory system for a store’s items, including produce and books. The starter program is an inventory system for only produce. 1. Include the price of an item by adding to the Item class the protected data member int priceInDollars that stores the price in dollars of an individual item. Write a public function SetPrice with a single int parameter prcInDllrs and returns nothing. SetPrice assigns the value of prcInDllrs to priceInDollars. Modify the AddItemToInventory...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT