Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We were asked to fill out the queue.cpp file that contains different functions We will be using this container for the next few assignments, adding

We were asked to fill out the queue.cpp file that contains different functions

We will be using this container for the next few assignments, adding to the container features common to the containers in the STL.

As a baseline, the queue must be able to push a new item into the back of the queue, and pop an item from the front of the queue.

In addition, Ive included an operator [] to access an element other than the first, additionally not removing the item from the queue. There is also a function empty which should return whether the queue currently contains any elements.

To implement a queue, we will be using a circular array. An array of values is reserved on the heap, but instead of the beginning of the structure always pointing to the first element of the array, the location of the first element changes, advancing every time an item is removed. In order to not run out of memory, when the end of the queue reaches the end of the array, the next item pushed is placed at the beginning of the reserved memory, instead of at the end.

This structure offers the benefit of constant time addition and removal of elements from the queue, while using a fixed amount of memory and location, offering good spatial locality. For our purposes, we will assume that the queue has a maximum size, which by default will be 10 elements, and can be assigned to a larger value in the qualified constructor which you pass the integer size of the memory to allocate.

Page 1 of 7

To complete this assignment, you must provide the implementation of each of the functions with the comment Requires Implementation. There is no main application in this assignment, instead your code will be loaded against a set of test cases in Gradescope to test whether the library is operating as intended. Additionally, the TAs will inspect the code to be sure that you are actually implementing the queue via a circular array, rather than something like a linked list.

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// queue.cpp shown bellow

image text in transcribed

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// queue.h shown below

image text in transcribed

queue::queue() // Requires implementation queue::queue (int sz) { // Requires implementation int& queue:: operator[](int index) // Requires implementation return index; void queue:: push(int value) // Requires implementation int queue::pop() // Requires implementation return 0; { bool queue:: empty() // Requires implementation return true; // done: // return 0; class queue private: int * arr; int begin; int end; int size; public: // Default constructor // Builds a queue containing room for 50 elements on the heap queue(); // Parametrized constructor // // Builds a queue containing room for sz elements on the heap queue(int sz); // operator[] // // Allows the user to extract a value in the queue // This value can be read and written, and is not removed from the queue // The index parameter specifies how far into the queue to go to find the element. index returns the first element of the queue A negative index, or an index greater than // the current length of the queue should throw an exception int& operator[](int index); // push(int) // Adds a new value to the queue, after the current last element. // The value parameter is used to create the new last element. If the queue is full, throw an exception and do not modify the queue. void push(int value); // pop() // // Removes the element at the current front of the queue, // making the second element the new first. // The value of the old first element should be returned. // If the queue is empty, throw an exception and do not modify the queue. int pop(); // empty() // Returns whether the queue is currently empty. // Returns true if the queue currently contains no elements. // Returns false if the queue currently contains at least one element. bool empty()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

=+ (c) From (18.10) deduce T(4) = VIT.

Answered: 1 week ago