C Program to Implement Queue Using Array

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 one to be removed. A queue can be implemented using an array, where elements are added at the rear (end) of the array and removed from the front (beginning) of the array.

Example:

  • Enqueue Operation: Adding elements [10, 20, 30] to the queue results in 10 being at the front and 30 at the rear.
  • Dequeue Operation: Removing the front element (10) from the queue leaves 20 at the front.

Problem Statement

Create a C program that:

  • Implements a queue using an array.
  • Performs queue operations such as enqueue, dequeue, and peek.
  • Displays the queue contents after each operation.

Solution Steps

  1. Include the Standard Libraries: Use #include <stdio.h> and #include <stdlib.h> for standard input-output functions.
  2. Define Constants and Global Variables: Define the maximum size of the queue, the queue array, and variables to keep track of the front and rear of the queue.
  3. Implement the Queue Operations:
  • Enqueue: Add an element to the rear of the queue.
  • Dequeue: Remove and return the front element from the queue.
  • Peek/Front: Return the front element without removing it.
  • IsEmpty: Check if the queue is empty.
  • IsFull: Check if the queue is full.
  1. Create a Main Function: In the main() function, allow the user to perform queue operations interactively.

C Program to Implement Queue Using Array

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

// Step 2: Define Constants and Global Variables
#define MAX 100  // Maximum size of the queue

int queue[MAX];
int front = -1;  // Initialize front to -1 to indicate an empty queue
int rear = -1;   // Initialize rear to -1 to indicate an empty queue

// Function to check if the queue is empty
int isEmpty() {
    return front == -1;
}

// Function to check if the queue is full
int isFull() {
    return rear == MAX - 1;
}

// Function to add an element to the queue (enqueue)
void enqueue(int value) {
    if (isFull()) {
        printf("Queue overflow! Cannot enqueue %d.\n", value);
        return;
    }
    if (front == -1) front = 0;  // If the queue is initially empty
    queue[++rear] = value;       // Increment rear and add the element
    printf("%d enqueued to the queue.\n", value);
}

// Function to remove and return the front element from the queue (dequeue)
int dequeue() {
    if (isEmpty()) {
        printf("Queue underflow! Cannot dequeue from an empty queue.\n");
        return -1;  // Return -1 to indicate queue underflow
    }
    int dequeued_value = queue[front];
    if (front >= rear) {
        // If there was only one element in the queue
        front = rear = -1;
    } else {
        front++;  // Move front to the next element
    }
    return dequeued_value;
}

// Function to return the front element of the queue without removing it
int peek() {
    if (isEmpty()) {
        printf("Queue is empty! Cannot peek.\n");
        return -1;  // Return -1 to indicate an empty queue
    }
    return queue[front];
}

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

int main() {
    int choice, value;

    while (1) {
        // Step 4: Provide a Menu for Queue Operations
        printf("\nQueue Operations Menu:\n");
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Peek\n");
        printf("4. Display\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to enqueue: ");
                scanf("%d", &value);
                enqueue(value);
                break;
            case 2:
                value = dequeue();
                if (value != -1) {
                    printf("Dequeued value: %d\n", value);
                }
                break;
            case 3:
                value = peek();
                if (value != -1) {
                    printf("Front value: %d\n", value);
                }
                break;
            case 4:
                display();
                break;
            case 5:
                exit(0);
            default:
                printf("Invalid choice! Please enter a valid option.\n");
        }
    }

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Define Constants and Global Variables

  • The maximum size of the queue (MAX) is defined as 100.
  • The queue is implemented as an array queue[MAX], and the variables front and rear are initialized to -1 to indicate that the queue is initially empty.

Function to Check if the Queue is Empty

  • The isEmpty function returns 1 if front is -1, indicating the queue is empty, otherwise returns 0.

Function to Check if the Queue is Full

  • The isFull function returns 1 if rear is equal to MAX - 1, indicating the queue is full, otherwise returns 0.

Function to Enqueue an Element into the Queue

  • The enqueue function adds an element to the rear of the queue if the queue is not full. If the queue was initially empty (front == -1), front is set to 0. The rear is incremented and the element is added to queue[rear].

Function to Dequeue an Element from the Queue

  • The dequeue function removes and returns the front element from the queue if the queue is not empty. If the queue only has one element, both front and rear are reset to -1 after dequeuing. Otherwise, front is incremented to point to the next element.

Function to Peek the Front Element of the Queue

  • The peek function returns the front element of the queue without removing it. It is useful for viewing the element at the front of the queue.

Function to Display the Queue Elements

  • The display function prints all the elements in the queue from front to rear.

Main Function

  • The main function provides a menu-driven interface allowing the user to perform various queue operations interactively.

Output Example

Example Output:

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter value to enqueue: 10
10 enqueued to the queue.

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter value to enqueue: 20
20 enqueued to the queue.

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 4
Queue elements: 10 20 

Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 2
Dequeued value: 10

Conclusion

This C program demonstrates how to implement a queue using an array. It includes basic queue operations such as enqueue, dequeue, peek, and display, making it a useful example for beginners learning about data structures in C programming.

Leave a Comment

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

Scroll to Top