Finish writing the queue member function called elements that returns the number
ID: 3810936 • Letter: F
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 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:
Here is the main program:
Explanation / Answer
Here is the code for Queue.h:
// implementation file for the queue class
#include <iostream>
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 ()
{
int temp = front;
int count = 0;
while(temp != rear)
{
temp = (temp + 1) % queue_size;
count++;
}
return count;
}
// prints the elements in the queue from first to last element. If the queue is empty, nothing
// is printed
void queue::print ()
{
int temp = front;
int count = 0;
while(temp != rear)
{
cout << data[temp+1] << " ";
temp = (temp + 1) % queue_size;
}
cout << endl;
}
// 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;
}
And the code for the main() is:
#include <cstdlib>
#include <iostream>
#include "queue.h"
/*
Testing program for queue member functions elements and print.
*/
using namespace std;
int main()
{
// declare a queue called qute
queue qute;
cout << " The queue to start is: ";
// print the queue
qute.print();
// print the number of elements in the queue
cout << " Number of elements to start is " << qute.elements() << endl;
// enqueue the letters H, I, P, P and O
qute.enqueue('H');
qute.enqueue('I');
qute.enqueue('P');
qute.enqueue('P');
qute.enqueue('O');
cout << " The queue after enqueueing is: ";
// print the queue
qute.print();
// print the number of elements in the queue
cout << " Number of elements after enqueueing is " << qute.elements() << endl;
// dequeue three elements
qute.dequeue();
qute.dequeue();
qute.dequeue();
// enqueue the letters K, E and R
qute.enqueue('K');
qute.enqueue('E');
qute.enqueue('R');
cout << " The final queue is: ";
// print the queue
qute.print();
// print the number of elements in the queue
cout << " Number of elements in final queue is " << qute.elements() << endl;
cout << " ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.