w3resource

C++ Queue Exercises: Find the second highest element of a queue

C++ Queue: Exercise-11 with Solution

Write a C++ program to find the second highest 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; // Stores the front index of the queue
    int rear; // Stores the rear index of the queue
    int arr[MAX_SIZE]; // Array to hold queue elements

  public:
    Queue() {
      front = -1; // Initialize front index to -1
      rear = -1; // Initialize rear index to -1
    }

    bool isFull() {
      return (rear == MAX_SIZE - 1); // Checks if the queue is full
    }

    bool isEmpty() {
      return (front == -1 && rear == -1); // Checks if the queue is empty
    }

    void enqueue(int x) {
      if (isFull()) {
        cout << "Error: Queue is full" << endl; // Displays error if the queue is full
        return;
      }
      if (isEmpty()) {
        front = 0;
        rear = 0;
      } else {
        rear++;
      }
      arr[rear] = x; // Adds an element to the rear of the queue
    }

    void dequeue() {
      if (isEmpty()) {
        cout << "Error: Queue is empty" << endl; // Displays error 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; // Displays error if the queue is empty
        return -1;
      }
      return arr[front]; // Returns the element at the front of the queue
    }

    void display() {
      if (isEmpty()) {
        cout << "Error: Queue is empty" << endl; // Displays error if the queue is empty
        return;
      }
      cout << "Queue elements are: ";
      for (int i = front; i <= rear; i++) {
        cout << arr[i] << " "; // Displays all elements in the queue
      }
      cout << endl;
    }

    int second_Highest(Queue q) {
      int max1 = -1; // Initialize the first maximum element
      int max2 = -1; // Initialize the second maximum element
      while (!q.isEmpty()) {
        int val = q.peek();
        q.dequeue();
        if (val > max1) {
          max2 = max1;
          max1 = val;
        } else if (val > max2) {
          max2 = val;
        }
      }
      return max2; // Return the second highest element in the queue
    }

};

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 sh_el = q.second_Highest(q); // Find the second highest element in the queue
  cout << "Second highest element of the said queue: " << sh_el << endl;
  cout << "\nInsert two more elements into the queue:" << endl;
  q.enqueue(6);
  q.enqueue(7);
  q.display();
  sh_el = q.second_Highest(q); // Find the second highest element again after inserting new elements
  cout << "Second highest element of the said queue: " << sh_el << endl;
  return 0;
}

Sample Output:

Initialize a Queue.

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

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

Flowchart:

Flowchart: Find the second highest element of a queue.
Flowchart: Find the second highest element of a queue.
Flowchart: Find the second highest element of a queue.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Find the minimum element of a queue.
Next C++ Exercise: Find the second lowest element of a queue.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.