C Queue

C Queue – Learn Queue Implementation in C

A queue is a linear data structure that follows the FIFO (First In, First Out) principle. Elements are added (enqueued) at the rear and removed (dequeued) from the front of the queue.

๐Ÿ”น What is a Queue?

A queue is a collection of elements that follows the FIFO principle, where the first element added to the queue will be the first one to be removed. The primary operations of a queue are enqueue (adding elements) and dequeue (removing elements).

๐Ÿ“ Example 1: Queue Structure and Operations

This example demonstrates how to create a simple queue structure in C, including the basic enqueue and dequeue operations.

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

#define MAX 5 // Define maximum size of queue

struct Queue {
    int arr[MAX];  // Array to store queue elements
    int front;     // Index of the front element
    int rear;      // Index of the rear element
};

// Function to initialize the queue
void initQueue(struct Queue* queue) {
    queue->front = -1; // Set front to -1 indicating an empty queue
    queue->rear = -1;  // Set rear to -1 indicating an empty queue
}

// Function to check if queue is full
int isFull(struct Queue* queue) {
    return queue->rear == MAX - 1; // Return 1 if queue is full
}

// Function to check if queue is empty
int isEmpty(struct Queue* queue) {
    return queue->front == -1; // Return 1 if queue is empty
}

// Function to enqueue (add) an element to the queue
void enqueue(struct Queue* queue, int value) {
    if (isFull(queue)) {
        printf("Queue Overflow!\n");
    } else {
        if (queue->front == -1) { // If queue is empty, set front to 0
            queue->front = 0;
        }
        queue->arr[++(queue->rear)] = value; // Add element to rear
        printf("Enqueued %d to queue\n", value);
    }
}

// Function to dequeue (remove) an element from the queue
int dequeue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue Underflow!\n");
        return -1; // Return -1 if queue is empty
    } else {
        int value = queue->arr[queue->front];
        if (queue->front == queue->rear) { // If only one element is left
            queue->front = queue->rear = -1; // Reset queue
        } else {
            queue->front++; // Increment front pointer
        }
        return value;
    }
}

int main() {
    struct Queue queue;
    initQueue(&queue);

    enqueue(&queue, 10);
    enqueue(&queue, 20);
    enqueue(&queue, 30);

    printf("Dequeued element: %d\n", dequeue(&queue));
    printf("Dequeued element: %d\n", dequeue(&queue));

    return 0;
}

Try It Now

๐Ÿ”น Front Operation

In addition to enqueue and dequeue, a queue also supports the front operation, which allows you to view the front element without removing it from the queue.

๐Ÿ“ Example 2: Front Operation

This example demonstrates how to implement a front function to view the front element of the queue without removing it.

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

#define MAX 5

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

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

int isFull(struct Queue* queue) {
    return queue->rear == MAX - 1;
}

int isEmpty(struct Queue* queue) {
    return queue->front == -1;
}

void enqueue(struct Queue* queue, int value) {
    if (isFull(queue)) {
        printf("Queue Overflow!\n");
    } else {
        if (queue->front == -1) {
            queue->front = 0;
        }
        queue->arr[++(queue->rear)] = value;
        printf("Enqueued %d to queue\n", value);
    }
}

int dequeue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue Underflow!\n");
        return -1;
    } else {
        int value = queue->arr[queue->front];
        if (queue->front == queue->rear) {
            queue->front = queue->rear = -1;
        } else {
            queue->front++;
        }
        return value;
    }
}

// Function to get the front element of the queue
int front(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue is empty!\n");
        return -1;
    } else {
        return queue->arr[queue->front];
    }
}

int main() {
    struct Queue queue;
    initQueue(&queue);

    enqueue(&queue, 10);
    enqueue(&queue, 20);
    enqueue(&queue, 30);

    printf("Front element is: %d\n", front(&queue));
    printf("Dequeued element: %d\n", dequeue(&queue));

    return 0;
}

Try It Now

๐Ÿ”น Queue Applications

Queues are widely used in various applications, including:

  • Task scheduling (e.g., round-robin scheduling in operating systems)
  • Handling requests in web servers (processing HTTP requests in a first-come, first-served manner)
  • Data streaming (handling streaming data in buffers)

๐ŸŽฏ Key Takeaways

  • A queue follows the FIFO principle: the first element added is the first to be removed.
  • Common queue operations include enqueue, dequeue, and front.
  • Queues are important for managing data in various real-time systems.

๐Ÿ“ Practice Time!

Try modifying the examples to experiment with different queue sizes, adding more elements, and performing operations in various orders!