Question: Assuming that you have the following base code already implemented, write a C + + program to find the sum of all elements of a

Assuming that you have the following base code already implemented, write a C++ program to find the sum of all elements of a queue.
#include
#include
using namespace std;
const int MAX_SIZE =100;
class Queue {
private:
int front; // Front index of the queue
int rear; // Rear index of the queue
int arr[MAX_SIZE]; // Array to store elements
public:
Queue(){
front =-1; // Initialize front index to -1
rear =-1; // Initialize rear index to -1
}
bool isFull(){
return (rear == MAX_SIZE -1); // Check if the queue is full
}
bool isEmpty(){
return (front ==-1 && rear ==-1); // Check if the queue is empty
}
void enqueue(int x){
if (isFull()){
// Display error message if queue is full
cout << "Error: Queue is full" << endl;
return;
}
if (isEmpty()){
front =0;
rear =0;
} else {
rear++;
}
arr[rear]= x; // Insert the element at the rear index
}
void dequeue(){
if (isEmpty()){
cout << "Error: Queue is empty" << endl; // Display error message if queue is empty
return;
}
if (front == rear){
front =-1;
rear =-1;
} else {
front++;
}
}
int peek(){
if (isEmpty()){
cout << "Error: Queue is empty" << endl; // Display error message if queue is empty
return -1;
}
return arr[front]; // Return the element at the front of the queue
}
void display(){
if (isEmpty()){
cout << "Error: Queue is empty" << endl; // Display error message if queue is empty
return;
}
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++){
cout << arr[i]<<""; // Display all elements in the queue
}
cout << endl;
}
// Function to calculate the sum of all elements in the queue
int sum_Queue(Queue & q)
//Enter your code here:
}
};

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 Programming Questions!