Implementation of queue using array
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100   // Define maximum size of the queue
typedef struct Queue {
    int front, rear, size;
    int array[MAX];
} Queue;
// Function to initialize the queue
void initQueue(Queue *q) {
    q->front = 0;
    q->rear = -1;
    q->size = 0;
}
// Function to check if the queue is empty
bool isEmpty(Queue *q) {
    return q->size == 0;
}
// Function to check if the queue is full
bool isFull(Queue *q) {
    return q->size == MAX;
}
// Function to add an element to the queue (enqueue)
void enqueue(Queue *q, int value) {
    if (isFull(q)) {
        printf("Queue is full. Cannot enqueue %d.\n", value);
        return;
    }
    q->rear = (q->rear + 1) % MAX;
    q->array[q->rear] = value;
    q->size++;
    printf("%d enqueued to queue.\n", value);
}
// Function to remove an element from the queue (dequeue)
int dequeue(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty. Cannot dequeue.\n");
        return -1; // Return a sentinel value to indicate the queue is empty
    }
    int value = q->array[q->front];
    q->front = (q->front + 1) % MAX;
    q->size--;
    return value;
}
// Function to get the front element of the queue
int front(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty.\n");
        return -1; // Return a sentinel value to indicate the queue is empty
    }
    return q->array[q->front];
}
// Function to get the rear element of the queue
int rear(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty.\n");
        return -1; // Return a sentinel value to indicate the queue is empty
    }
    return q->array[q->rear];
}
// Function to print the queue elements
void printQueue(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty.\n");
        return;
    }
    printf("Queue elements are:\n");
    for (int i = 0; i < q->size; i++) {
        printf("%d ", q->array[(q->front + i) % MAX]);
    }
    printf("\n");
}
int main() {
    Queue q;
    initQueue(&q);
    enqueue(&q,   10);
    enqueue(&q,   20);
    enqueue(&q,   30);
    enqueue(&q,   40);
    printf("Front element: %d\n", front(&q));
    printf("Rear element: %d\n", rear(&q));
    printQueue(&q);
    printf("%d dequeued from queue.\n", dequeue(&q));
    printf("%d dequeued from queue.\n", dequeue(&q));
    printQueue(&q);
    return 0;
}