Question: Download the Queue.zip file, which provides a simple framework for a queue data structure. Implement the details for the operations for this basic queue data
Download the Queue.zip file, which provides a simple framework for a queue data structure. Implement the details for the operations for this basic queue data structure that will use an array of elements of type string. We will not use templates for this implementation. Provide the implementations details for the following class methods (i.e. operations): initialize(), empty(), enqueue(string element), dequeue(), and nextElement(). Descriptions of most these methods are provided in the source code and in supplementary module in the Learning Activities area. The code in the Driver.cpp file will remain the same. Hint: Again, you only need to modify the areas in the Queue.h file by adding the necessary code to implement the TODO areas as noted in the comments. The prototype for the functions and everything else in the program must remain unchanged. You must use the function signature for your implementation. Output: The output for the program after the functions are implemented should appear as follows: Queue properties: empty = 1 max size = 35 current size = 0 next element = [None] Queue properties: empty = 0 3 max size = 35 current size = 13 next element = life Queue elements: life is a succession of lessons which must be lived to be understood Queue properties: empty = 1 max size = 35 current size = 0 next element = [None] Queue elements: The queue is empty! Queue properties: empty = 0 max size = 35 current size = 8 next element = problems Queue elements: problems are not stop signs they are guidelines ** Press any key to continue **
/** * * Queue.h - This file contains the definition and implementation * for the queue class. * * TODO: Include your name and course number here */
#include
using namespace std;
class Queue { public: Queue(int maxSize); ~Queue(); void initialize(); bool empty(); void enqueue(string element); string dequeue(); string nextElement(); int getSize(); int getMaxSize(); void properties(); void print();
private: int maxSize; // The total number of items the queue can // hold. 0 is the first item, so assume a // maxSize of -1 means the queue is empty. int size; // The current number of items in the queue. // Again the size is zero based, so // assume a size of -1 means the queue // is empty string *queue; // Holds a queue of strings };
/* * The Queue class implementatoin begins here. */
Queue::Queue(int maxSize) { this->maxSize= maxSize; queue = new string[maxSize]; initialize(); }
Queue::~Queue() { maxSize = -1; size = -1; delete [] queue; }
void Queue::initialize() { // TODO: // This method deletes all of the items in the // queue. It should also adjust the size variable // // Hint: use a for-loop to set every element in the // queue to the empty string: "" and set the size to // -1.
return; }
bool Queue::empty() { // TODO: // This method checks to see if the queue is empty. // If it is, (i.e. the size is -1) then return true. // If the queue is not return false.
return true; }
void Queue::enqueue(string element) { // TODO: // This method adds the element to the end of the queue. // You should first check if the value of size+1 does // not exceed the maximum queue size. If it does do not // add the element.
return; }
string Queue::dequeue() { // Temporary variable to hold the dequeued element string element = "";
// TODO: // This method removes and returns the first element // in the queue. // // 1. You should first check if the queue is empty, if it is // return the empty string "". // 2. If the queue is not empty set the element variable equal // to the value of the first element in the queue. // 3. The move the proceeding element in the queue down by one // position. // 4. Adjust the size of the queue down by 1.
return element; }
string Queue::nextElement() { string element = "[None]";
// TODO: // This method should only return the element that // will be dequeued next in the queue . This method // should not dequeue the element. It should return // "[None]" if the queue is empty. return element; }
int Queue::getSize() { return (size+1); }
int Queue::getMaxSize() { return maxSize; }
void Queue::properties() { cout << "Queue properties:" << endl; cout << " empty = " << empty() << endl; cout << " max size = " << getMaxSize() << endl; cout << " current size = " << getSize() << endl; cout << " next element = " << nextElement() << endl; cout << endl;
return; }
void Queue::print() { cout << "Queue elements:" << endl;
if (empty()) { cout << " The queue is empty!" << endl; cout << endl;
return; }
while (empty() == false) { string element = dequeue();
cout << " " << element << endl;
if (element.size() == 0) break; }
cout << endl; return; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
