C++ ONLY PLEASE 01. implement a class function in the cqueue class to print all
ID: 3817135 • Letter: C
Question
C++ ONLY PLEASE
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
cqeue.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;
}
}
cqueue.h
#ifndef cqueue_H
#define cqueue_H
class cqueue
{
int q[10];
int front;
int rear;
public:
cqueue();
void enqueue(int);
int dequeue();
};
#endif
testqueue.cpp
#include<iostream>
#include "cqueue.h"
using namespace std;
int main()
{
cqueue q;
char c = 'c'; //enter 's' to end
int j = 20;
while(c != 's')
{
cout << "enter 'e' to enqueue "<< endl << "enter 'd' to dequeue" << endl << "enter 's' to stop"<< endl;
cin >> c;
if (c == 'e')
q.enqueue(j++);
else if (c == 'd'){
int v = q.dequeue();
cout << "Current Value: "<< v << endl;
}
else
break;
}
return 0;
}
Explanation / Answer
Did all 4, (as dqueue class was not provided I added dequeueAll to cqueue class)
testcqueue.cpp
#include<iostream>
#include "cqueue.h"
using namespace std;
int main()
{
cqueue q;
char c = 'c'; //enter 'q' to end
while(c != 'q')
{
cout << "enter 'e' to enqueue "<< endl;
cout << "enter 'd' to dequeue" << endl;
cout << "enter 'p' to print queue" << endl;
cout << "enter 's' to print current size of queue" << endl;
cout << "enter 'q' to stop"<< endl;
cin >> c;
if (c == 'e')
{
int val;
cout << "Enter value to be enqueued: ";
cin >> val;
q.enqueue(val);
}
else if (c == 'd'){
int v = q.dequeue();
cout << "Current Value: "<< v << endl;
}
else if (c == 'p'){
q.print();
}
else if (c == 's'){
cout << "Current size of queue is : " << q.currentSize() << endl;
}
else
break;
}
return 0;
}
cqueue.h
#ifndef cqueue_H
#define cqueue_H
class cqueue
{
int q[10];
int front;
int rear;
int size;
public:
cqueue();
void enqueue(int);
int dequeue();
void dequeueAll();
void print();
int currentSize();
};
#endif
cqueue.cpp
#include "cqueue.h"
#include <iostream>
using namespace std;
cqueue :: cqueue()
{
front = rear = -1;
size = 0;
}
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;
}
size++;
}
int cqueue :: dequeue ()
{
if ((front == rear) && (rear == -1)) {
cout << "The queue is empty!"<< endl;
return -1;
}
else
{
size--;
int x = q[front];
if(front == rear)
{ front = rear = -1;}
else
front = (front + 1) % 10;
return x;
}
}
int cqueue::currentSize()
{
return size;
}
void cqueue::print()
{
int f = front;
int r = rear;
if ((front == rear) && (rear == -1)) {
cout << "The queue is empty!"<< endl;
}
while(!(f==r && r == -1))
{
int x = q[f];
if(f == r)
{ f = r = -1;}
else
f = (f + 1) % 10;
cout << x << " ";
}
cout << endl;
}
void dequeueAll()
{
while (!((front == rear) && (rear == -1))) {
cout << dequeu() << " ";
}
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.