Question: Finish writing the queue member function called elements that returns the number of elements in a queue. It should use the loop described above to

Finish writing the queue member function called elements that returns the number of elements in a queue. It should use the loop described above to move through the elements in the queue to count them. It should return the number counted. The function should work if the queue is empty (should return 0).

int elements ();

Finish writing the queue member function called print that prints the elements in the queue on a single line. It should use the loop described above to move through the elements in the queue to print them. If the queue is empty, it should not print anything.

void print ();

Make the following changes to the application file (there are comments to help instruct you where to place each item):

Declare a queue called qute.

print the queue.

Print the number of elements in the queue (add to the cout).

enqueue the characters H, I, P, P and O on the queue.

print the queue.

Print the number of elements in the queue.

dequeue the queue three times.

enqueue the characters K, E and R on the queue.

print the queue.

Print the number of elements in the queue.

Here is the queue class:

// implementation file for the queue class #include  using namespace std; const int queue_size = 6; class queue { private: char data [queue_size]; // elements in the queue int front, // index to the front of the queue, indexes one previous // to actual front element (remove) rear; // index of the rear element; public: queue (); // create an empty queue void enqueue (char item); // adds an element to the rear char dequeue (); // removes an element from the front bool empty (); // returns true for an empty queue bool full (); // returns true for a full queue int elements (); // returns the number of elements in the queue void print (); // prints the queue from front to rear }; // returns the number of elements in the queue. If queue is empty, it returns 0. int queue::elements () { } // prints the elements in the queue from first to last element. If the queue is empty, nothing // is printed void queue::print () { } // constructor creates an empty queue queue::queue () { front = 0; rear = 0; } // enqueue adds an element to the rear of the queue void queue::enqueue (char item) { // if full, can't add another element if (full ()) { cout << " Queue Error: Can't add to a full queue"; cout << " Queue will not be changed"; } else // ok to add an item { rear = (rear + 1) % queue_size; data [rear] = item; } } // dequeue removes an element from the front of the queue char queue::dequeue () { // if the queue is empty, we can't remove a value if (empty ()) { cout << " Queue Error: Can't remove from an empty queue"; cout << " Returning a blank"; return ' '; } else // ok to remove a value { front = (front + 1) % queue_size; return data [front]; } } // empty returns true if the queue is empty bool queue::empty () { return front == rear; } // full returns true if the queue is full bool queue::full () { return (rear + 1) % queue_size == front; } 

Here is the main program:

#include  #include  #include "queue.h" /* Testing program for queue member functions elements and print. */ using namespace std; int main() { // declare a queue called qute cout << " The queue to start is: "; // print the queue // print the number of elements in the queue cout << " Number of elements to start is " << // enqueue the letters H, I, P, P and O cout << " The queue after enqueueing is: "; // print the queue // print the number of elements in the queue cout << " Number of elements after enqueueing is " << // dequeue three elements // enqueue the letters K, E and R cout << " The final queue is: "; // print the queue // print the number of elements in the queue cout << " Number of elements in final queue is " << cout << " "; return 0; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!