C Program to Implement Queue Using Arrays

1. Introduction

A queue is a linear data structure that follows the First In First Out (FIFO) principle. This means that the first element added to the queue will be the first element to be removed. In this post, we will implement a queue using arrays, covering basic operations like enqueue (to add an element at the rear end) and dequeue (to remove the front element).

2. Program Overview

In this program, we will:

1. Define the maximum size of the queue.

2. Implement the following queue operations:

- Enqueue: To add an element at the rear end of the queue.

- Dequeue: To remove the front element from the queue.

- Peek: To view the front element without removing it.

- Display: To display the elements of the queue.

3. Code Program

#include <stdio.h>
#define MAX_SIZE 10

// Queue and its front and rear
int queue[MAX_SIZE];
int front = -1, rear = -1;

// Function to enqueue an element to the queue
void enqueue(int value) {
    if ((front == 0 && rear == MAX_SIZE - 1) || (rear == (front - 1) % (MAX_SIZE - 1))) {
        printf("Queue Overflow!\n");
        return;
    } else if (front == -1) {
        front = rear = 0;
        queue[rear] = value;
    } else if (rear == MAX_SIZE - 1 && front != 0) {
        rear = 0;
        queue[rear] = value;
    } else {
        rear++;
        queue[rear] = value;
    }
}

// Function to dequeue an element from the queue
int dequeue() {
    int data;
    if (front == -1) {
        printf("Queue is Empty!\n");
        return -1;
    }
    data = queue[front];
    if (front == rear) {
        front = -1;
        rear = -1;
    } else if (front == MAX_SIZE - 1) {
        front = 0;
    } else {
        front++;
    }
    return data;
}

// Function to get the front element of the queue
int peek() {
    if (front == -1) {
        printf("Queue is empty!\n");
        return -1;
    }
    return queue[front];
}

// Function to display the queue
void display() {
    if (front == -1) {
        printf("Queue is Empty!\n");
        return;
    }
    for (int i = front; i <= rear; i++) {
        printf("%d ", queue[i]);
    }
    printf("\n");
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    printf("Front element: %d\n", peek());
    display();
    printf("Dequeued element: %d\n", dequeue());
    display();
    return 0;
}

Output:

Front element: 10
10 20 30
Dequeued element: 10
20 30

4. Step By Step Explanation

1. We define a queue array of MAX_SIZE and initialize front and rear pointers to -1 indicating an empty queue.

2. The enqueue function adds an element at the rear end of the queue. It checks for Queue Overflow before adding.

3. The dequeue function removes and returns the front element from the queue. It checks if the queue is empty before proceeding.

4. The peek function returns the front element without removing it from the queue. It checks if the queue is empty before proceeding.

5. The display function prints the current elements of the queue from front to rear.

6. In the main function, we demonstrate enqueuing elements onto the queue, viewing the front element, dequeuing an element, and displaying the queue.

Comments