﻿ C++ Nth element from the bottom of a queue

C++ Queue Exercises: Nth element from the bottom of a queue

C++ Queue: Exercise-22 with Solution

Write a C++ program to get the Nth element from the bottom of a queue.

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
}

int find_Nth_From_Bottom(Queue q, int n) {
int size = 0;
Queue temp;

// Find the size of the queue
while (!q.isEmpty()) {
temp.enqueue(q.peek());
q.dequeue();
size++;
}

// If the given index is greater than the size of the queue
if (n > size) {
cout << "Error: Queue does not have " << n << " elements." << endl;
return -1;
}

// Copy the elements back to the original queue
for (int i = 0; i < size; i++) {
q.enqueue(temp.peek());
temp.dequeue();
}

// Dequeue the first (size - n) elements
for (int i = 0; i < size - n; i++) {
q.enqueue(q.peek());
q.dequeue();
}

// Return the nth element from the top
return q.peek();
}

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 a Queue." << endl;
Queue q1; // Creating an instance of Queue
cout << "\nInsert some elements into the queue:" << endl;
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
q1.enqueue(6);
q1.display(); // Display elements of the queue
int n = 2;
cout << "\nNth element from bottom is " << q1.find_Nth_From_Bottom(q1, n) << " Where n = " << n << endl;
n = 3;
cout << "\nNth element from bottom is " << q1.find_Nth_From_Bottom(q1, n) << " Where n = " << n << endl;
n = 4;
cout << "\nNth element from bottom is " << q1.find_Nth_From_Bottom(q1, n) << " Where n = " << n << endl;
return 0;
}
```
```

Sample Output:

```Initialize a Queue.

Insert some elements into the queue:
Queue elements are: 1 2 3 4 5 6

Nth element from bottom is 5 Where n = 2

Nth element from bottom is 4 Where n = 3

Nth element from bottom is 3 Where n = 4
```

Flowchart:

CPP Code Editor: