A queue is a linear data structure that follows the First In First Out (FIFO) principle. The element inserted first is the one that is removed first. A queue typically supports two primary operations:

  • Enqueue: Add an element to the rear (end) of the queue.
  • Dequeue: Remove an element from the front of the queue.

In this program, we will implement a queue using arrays. We will also implement additional helper functions to check if the queue is empty or full and to display the elements of the queue.

Program Explanation

We will use an array to represent the queue and two pointers: front and rear.

  • front: Points to the front of the queue, where elements are removed (dequeue).
  • rear: Points to the rear of the queue, where elements are added (enqueue).

The queue operations follow these rules:

  1. Enqueue: Add an element at the rear and move the rear pointer forward.
  2. Dequeue: Remove an element from the front and move the front pointer forward.
  3. The queue is considered full if the rear pointer reaches the maximum size of the queue. It is considered empty if the front pointer exceeds the rear pointer.

C Program Code


#include <stdio.h>
#include <stdlib.h>

#define MAX 5  // Define the maximum size of the queue

// Queue structure
struct Queue {
    int arr[MAX];
    int front;
    int rear;
};

// Initialize the queue
void initQueue(struct Queue *q) {
    q->front = -1;
    q->rear = -1;
}

// Check if the queue is full
int isFull(struct Queue *q) {
    return q->rear == MAX - 1;
}

// Check if the queue is empty
int isEmpty(struct Queue *q) {
    return q->front == -1 || q->front > q->rear;
}

// Enqueue an element into the queue
void enqueue(struct Queue *q, int value) {
    if (isFull(q)) {
        printf("Queue is full. Cannot enqueue %d.\n", value);
        return;
    }
    
    if (q->front == -1) {
        q->front = 0;  // Set front to 0 if this is the first element
    }
    
    q->rear++;
    q->arr[q->rear] = value;
    printf("Enqueued %d\n", value);
}

// Dequeue an element from the queue
int dequeue(struct Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty. Cannot dequeue.\n");
        return -1;
    }

    int dequeuedValue = q->arr[q->front];
    q->front++;

    if (q->front > q->rear) {
        // Reset queue if all elements are dequeued
        q->front = q->rear = -1;
    }

    printf("Dequeued %d\n", dequeuedValue);
    return dequeuedValue;
}

// Display the elements of the queue
void displayQueue(struct Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty.\n");
        return;
    }

    printf("Queue elements: ");
    for (int i = q->front; i <= q->rear; i++) {
        printf("%d ", q->arr[i]);
    }
    printf("\n");
}

// Main driver function
int main() {
    struct Queue q;
    initQueue(&q);

    enqueue(&q, 10);  // Enqueue 10
    enqueue(&q, 20);  // Enqueue 20
    enqueue(&q, 30);  // Enqueue 30
    enqueue(&q, 40);  // Enqueue 40
    enqueue(&q, 50);  // Enqueue 50 (queue is full now)

    displayQueue(&q);  // Display the queue

    dequeue(&q);  // Dequeue an element
    dequeue(&q);  // Dequeue another element

    displayQueue(&q);  // Display the queue again

    enqueue(&q, 60);  // Enqueue 60
    displayQueue(&q);  // Display the queue after enqueueing 60

    return 0;
}
    

Code Explanation

1. Queue Structure: The queue is represented as an array of fixed size MAX, and two pointers front and rear are used to track the front and rear elements of the queue.

2. Enqueue Operation: The function enqueue() inserts elements at the rear of the queue. If the queue is full (i.e., rear == MAX - 1), new elements cannot be added.

3. Dequeue Operation: The function dequeue() removes elements from the front of the queue. If the queue becomes empty (i.e., front > rear), both the front and rear pointers are reset.

4. Display Operation: The function displayQueue() prints the elements of the queue from front to rear.

Sample Output


Enqueued 10
Enqueued 20
Enqueued 30
Enqueued 40
Enqueued 50
Queue is full. Cannot enqueue 60.
Queue elements: 10 20 30 40 50 
Dequeued 10
Dequeued 20
Queue elements: 30 40 50 
Enqueued 60
Queue elements: 30 40 50 60 
    

Conclusion

This C program demonstrates how to implement a queue using arrays. The queue operates using the FIFO principle and allows for the addition of elements at the rear (enqueue) and removal from the front (dequeue). The queue is easy to implement with arrays, though care must be taken to handle queue overflow and underflow conditions.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)