01. implement a class function in the cqueue class to print all the elements fro
ID: 3817652 • Letter: 0
Question
01. implement a class function in the cqueue class to print all the elements from thequeue (without dequeueing them!) [10 points]
02. implement a class function in the cqueue class to count the current size of the queue(if you have inserted 5 elements and dequeued 3, the current size is 2, initially the queue size is 0) [10 points]
03. implement a class function for dequeue class to dequeue all the elements from the queue (print each element as you delete them) [10 points]
04. modify the while loop in the main function to take the input from user while enqueuing new value. [05 Points]
Add 3 test cases for each completed task with your submitted code.
Bonus:
Pick any 2 out of Task 01, 02, and 03. Each carries 10 points. If you can complete all three (01, 02, 03), the third one will be bonus point and will be added to any of the previous recitation quiz of your choice (mention which one to pick!).
Explanation / Answer
-------------------------cqueue.h-------------------------------------------
#ifndef cqueue_H
#define cqueue_H
class cqueue
{
int q[10];
int front;
int rear;
public:
cqueue();
void enqueue(int);
int dequeue();
void display();
void count();
void dequeueAll();
};
#endif
-----------------------------------cqueue.cpp-----------------
#include "cqueue.h"
#include <iostream>
using namespace std;
cqueue :: cqueue()
{
front = rear = -1;
}
void cqueue :: enqueue(int x)
{
if ((rear+1) % 10 == front)
{
cout << "The queue is full"<< endl;
return;
}
else
{
rear = (rear + 1) % 10;
q[rear] = x;
if (front == -1)
front = 0;
}
}
int cqueue :: dequeue ()
{
if ((front == rear) && (rear == -1)) {
cout << "The queue is empty!"<< endl;
return -1;
}
else
{
int x = q[front];
if(front == rear)
{ front = rear = -1;}
else
front = (front + 1) % 10;
return x;
}
}
void cqueue :: display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty ";
return;
}
cout<<"Queue elements : ";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout<<q[front_pos]<<" ";
front_pos++;
}
}
else
{
while (front_pos <= 9)
{
cout<<q[front_pos]<<" ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout<<q[front_pos]<<" ";
front_pos++;
}
}
cout<<endl;
}
void cqueue::count()
{
int front_pos = front, rear_pos = rear;
int c=0;
cout<<"No of elements in the queue : ";
if(front==-1){
cout<<c<<endl;
return ;}
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
c++;
front_pos++;
}
cout<<c<<endl;
return;
}
else
{
while (front_pos <= 9)
{
c++;
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
c++;
front_pos++;
}
cout<<c<<endl;
return;
}
}
void cqueue::dequeueAll()
{
int front_pos = front, rear_pos = rear;
if ((front == rear) && (rear == -1)) {
cout << "The queue is empty!"<< endl;
return ;
}
while(front!=-1)
{
int x = q[front];
cout<<"Deleted element: "<<x<<endl;
if(front == rear)
{ front = rear = -1;}
else
front = (front + 1) % 10;
}
}
-----------------------------------main.cpp--------------------------------------------------------
#include<iostream>
#include "cqueue.h"
#include<stdlib.h>
using namespace std;
int main()
{
cqueue q;
int j=20;
int choice;
do{
cout<<" --------QUEUE OPERATIONS----------- ";
cout<<"1.Insert ";
cout<<"2.Delete ";
cout<<"3.Display ";
cout<<"4.Count ";
cout<<"5.DeleteAll ";
cout<<"6.Exit ";
cout<<"-----------------------";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
q.enqueue(j++);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
q.count();
break;
case 5:
q.dequeueAll();
break;
case 6:
exit(0);
break;
default:
cout<<" Invalid choice: ";
break;
}
}while(choice!=6);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.