Write the expression for last_index + 1 with wrap-around . Make the following ch
ID: 3762762 • Letter: W
Question
Write the expression for last_index + 1 with wrap-around.
Make the following changes to the implementation file:
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.
the queue.
Print the number of elements in the queue (add to the cout).
the characters H, I, P, P and O on the queue.
the queue.
Print the number of elements in the queue.
the queue three times.
the characters K, E and R on the queue.
the queue.
Print the number of elements in the queue.
Implementation file/MAIN FILE
______________________________________________________________________________
QUEUE.H FILE
_________________________________________________________________________
Explanation / Answer
include <iostream>
using namespace std;
const int queue_size = 6;
class queue
{
private:
char data [queue_size];
int front,
rear;
public:
queue ();
void enqueue (char item);
char dequeue ();
bool empty ();
bool full ();
int elements ();
void print ();
};
int queue::elements ()
{
int i;
if(empty())
return 0;
else
for (i = front+1; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
return i;
}
void queue::print ()
{
int i;
if(!empty())
for (i = front+1; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
cout << data[i] << " ";
}
queue::queue ()
{
front = 0;
rear = 0;
}
void queue::enqueue (char item)
{
if (full ())
{
cout << " Queue Error: Can't add to a full queue";
cout << " Queue will not be changed";
}
else
{
rear = (rear + 1) % queue_size;
data [rear] = item;
}
}
char queue::dequeue ()
{
if (empty ())
{
cout << " Queue Error: Can't remove from an empty queue";
cout << " Returning a blank";
return ' ';
}
else
{
front = (front + 1) % queue_size;
return data [front];
}
}
bool queue::empty ()
{
return front == rear;
}
bool queue::full ()
{
return (rear + 1) % queue_size == front;
}
int main()
{
queue q;
q.enqueue('a');
q.enqueue('b');
q.enqueue('c');
q.enqueue('d');
q.enqueue('e');
q.print();
cout << endl;
cout << " There are " << q.elements() << " elements.";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.