w3resource

C++ Queue Exercises: Intersection of two queues

C++ Queue: Exercise-26 with Solution

Write a C++ program to find the intersection of two queues.

Sample Solution:

C Code:

#include <iostream> // Including necessary library for input and output operations

using namespace std;

const int MAX_SIZE = 100; // Maximum size for the queue

class Queue {
private:
    int front; // Front of the queue
    int rear; // Rear of the queue
    int arr[MAX_SIZE]; // Array to store queue elements

public:
    Queue() {
        front = -1; // Initializing front index to -1
        rear = -1; // Initializing 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 if the queue is full
            return;
        }
        if (isEmpty()) {
            front = 0;
            rear = 0;
        } else {
            rear++;
        }
        arr[rear] = x; // Add an element to the rear of the queue
    }

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

    Queue findIntersection(Queue q1, Queue q2) {
        Queue q3; // Initialize an empty queue to store the intersection
        while (!q1.isEmpty()) {
            int x = q1.peek(); // Get the element at the front of q1
            q1.dequeue(); // Remove the element from q1
            Queue temp = q2; // Create a temporary queue for q2
            while (!temp.isEmpty()) {
                int y = temp.peek(); // Get the element at the front of the temporary queue
                temp.dequeue(); // Remove the element from the temporary queue
                if (x == y) {
                    q3.enqueue(x); // If x is equal to y, enqueue x to q3
                    break;
                }
            }
        }
        return q3; // Return the intersection queue
    }

    void display() {
        if (isEmpty()) {
            cout << "Error: Queue is empty" << endl; // Display error 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 main() {
    cout << "Initialize three Queues." << endl;
    Queue q1, q2, q3; // Creating instances of Queue
    q1.enqueue(1);
    q1.enqueue(2);
    q1.enqueue(3);
    q2.enqueue(1);
    q2.enqueue(2);
    q2.enqueue(4);
    q3.enqueue(3);
    q3.enqueue(2);
    q3.enqueue(1);
    cout << "Queue-1" << endl;
    q1.display(); // Display elements of Queue-1
    cout << "Queue-2" << endl;
    q2.display(); // Display elements of Queue-2
    cout << "Queue-3" << endl;
    q3.display(); // Display elements of Queue-3
    cout << "\nIntersection of two queues q1 and q2:" << endl;
    Queue result = q1.findIntersection(q1, q2); // Get intersection of q1 and q2
    result.display(); // Display the intersection
    cout << "\nIntersection of two queues q2 and q3:" << endl;
    Queue result1 = q1.findIntersection(q2, q3); // Get intersection of q2 and q3
    result1.display(); // Display the intersection
    cout << "\nIntersection of two queues q3 and q1:" << endl;
    Queue result2 = q1.findIntersection(q3, q1); // Get intersection of q3 and q1
    result2.display(); // Display the intersection
    return 0;
}

Sample Output:

Initialize three Queues.
Queue-1
Queue elements are: 1 2 3 
Queue-2
Queue elements are: 1 2 4 
Queue-3
Queue elements are: 3 2 1 

Intersection of two queues q1 and q2:
Queue elements are: 1 2 

Intersection of two queues q2 and q3:
Queue elements are: 1 2 

Intersection of two queues q3 and q1:
Queue elements are: 3 2 1

Flowchart:

Flowchart: Intersection of two queues.
Flowchart: Intersection of two queues.
Flowchart: Intersection of two queues.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Check if two queues are equal.
Next C++ Exercise: Union of two queues.

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://www.w3resource.com/cpp-exercises/queue/cpp-queue-exercise-26.php