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...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
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.          ...
/* 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);...
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;...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
"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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT