w3resource

C++ Queue Exercises: Find the maximum element of a queue

C++ Queue: Exercise-9 with Solution

Write a C++ program to find the maximum element of a queue.

Sample Solution:

C Code:

#include <iostream>
#include <map>
#include <cmath>

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()) {
        cout << "Error: Queue is full" << endl; // Display error message if the queue is full
        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 the 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 the 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 the 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;
    }

    int findMax(Queue q) {
      int max = q.peek(); // Initialize max with the first element of the queue
      while (!q.isEmpty()) {
        int curr = q.peek(); // Get the current element
        if (curr > max) {
          max = curr; // Update max if the current element is greater
        }
        q.dequeue(); // Remove the element from the queue
      }
      return max; // Return the maximum element
    }
};

int main() {
  cout << "Initialize a Queue." << endl;
  Queue q;
  cout << "\nInsert some elements into the queue:" << endl;
  q.enqueue(1);
  q.enqueue(2);
  q.enqueue(3);
  q.enqueue(4);
  q.enqueue(5);
  q.display();
  int max = q.findMax(q); // Find the maximum element in the queue
  cout << "Maximum element of the said queue: " << max << endl;
  cout << "\nInsert two more elements into the queue:" << endl;
  q.enqueue(-6);
  q.enqueue(7);
  q.display();
  max = q.findMax(q); // Find the maximum element again after inserting new elements
  cout << "Maximum element of the said queue: " << max << endl;
  return 0;
}

Sample Output:

Initialize a Queue.

Insert some elements into the queue:
Queue elements are: 1 2 3 4 5 
Maximum element of the said queue: 5

Insert two more elements into the queue:
Queue elements are: 1 2 3 4 5 -6 7 
Maximum element of the said queue: 7

Flowchart:

Flowchart: Find the maximum element of a queue.
Flowchart: Find the maximum element of a queue.
Flowchart: Find the maximum element of a queue.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Mean, variance and standard deviation of all elements of a queue.
Next C++ Exercise: Find the minimum element of a queue.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/queue/cpp-queue-exercise-9.php