C Exercises: Find the minimum element in a queue
10. Queue Minimum Element
Write a C program to find the minimum element in a queue.
Sample Solution:
C Code:
// Header file inclusion for standard input and output operations
#include <stdio.h>
#define MAX_SIZE 100
int queue[MAX_SIZE]; // Array to store elements of the queue
int front = -1, back = -1; // Initialize front and back pointers
// Function to insert an element into the queue
void enqueue(int item) {
    if (back == MAX_SIZE - 1) { // Check if the queue is full
        printf("Error: Queue is full\n"); // Print error message if the queue is full
        return;
    }
    if (front == -1) { // Check if the queue is empty
        front = 0; // If empty, set front to 0
    }
    back++; // Increment the back pointer
    queue[back] = item; // Add the item to the queue
}
// Function to remove an element from a queue
int dequeue() {
    if (front == -1 || front > back) { // Check if the queue is empty
        printf("Error: Queue is empty\n"); // Print error message if the queue is empty
        return -1; // Return -1 to indicate an empty queue
    }
    int item = queue[front]; // Get the front element of the queue
    front++; // Move the front pointer to the next element
    return item; // Return the removed element
}
// Function to find the minimum value in the queue
int findMin() {
    if (front == -1 || front > back) { // Check if the queue is empty
        printf("Error: Queue is empty\n"); // Print error message if the queue is empty
        return -1; // Return -1 to indicate an empty queue
    }
    int min = queue[front]; // Initialize min with the first element of the queue
    // Loop through the elements of the queue to find the minimum value
    for (int i = front; i <= back; i++) {
        if (queue[i] < min) { // Compare the current element with min
            min = queue[i]; // Update min if the current element is smaller
        }
    }
    return min; // Return the minimum value
}
// Function to display the elements in the queue
void display() {
    printf("Queue elements are: "); // Print message to indicate queue elements
    for (int i = front; i <= back; i++) { // Loop through the elements of the queue
        printf("%d ", queue[i]); // Print the current element
    }
    printf("\n"); // Print a newline after displaying all elements
}
int main() {
    // Insert some elements into the queue.
    enqueue(1);
    enqueue(2);
    enqueue(3);
    enqueue(4);
    enqueue(5);
    // Display the elements in the queue.
    display();
    // Find the minimum value in the queue.
    int min_val = findMin();
    printf("Minimum value in the queue is: %d\n", min_val);
    printf("\nRemove 2 elements from the said queue:\n");
    dequeue();
    dequeue();
    // Display the elements in the queue.
    display();
    // Recalculate the minimum value in the queue.
    min_val = findMin();
    printf("Minimum value in the queue is: %d\n", min_val);
    printf("\nInsert 3 more elements:\n");
    enqueue(600);
    enqueue(-427);
    enqueue(519);
    // Display the elements in the queue.
    display();
    // Recalculate the minimum value in the queue.
    min_val = findMin();
    printf("Minimum value in the queue is: %d\n", min_val);
    return 0; // Return 0 to indicate successful execution
}
Output:
Queue elements are: 1 2 3 4 5 Minimum value in the queue is: 1 Remove 2 elements from the said queue: Queue elements are: 3 4 5 Minimum value in the queue is: 3 Insert 3 more elements: Queue elements are: 3 4 5 600 -427 519 Minimum value in the queue is: -427
Flowchart
 
 
 
For more Practice: Solve these Related Problems:
- Write a C program to implement a queue that efficiently retrieves the minimum element in O(1) time using an auxiliary structure.
- Write a C program to implement a linked list queue that dynamically maintains the minimum element after each operation.
- Write a C program to recalculate the minimum element in a circular queue after multiple dequeues using lazy updates.
- Write a C program to find the minimum element within a user-defined subrange in an array-based queue.
Go to:
PREV : Queue Maximum Element. 
NEXT : Delete nth Element in Queue.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
