Implementation of Queue Using Array in C, C++, and Java

A queue is a linear data structure that operates on the principle of First In, First Out (FIFO). It is widely used in various computing problems where tasks or resources are processed in the same order they are added. The queue can be implemented in different ways, and one of the most efficient ways is through an array.

This article explores the concept of a queue using an array, its implementation in multiple programming languages such as C, C++, and Java, and its advantages, disadvantages, and common applications.

Article Metadata:

Table of Contents

  1. What is Queue Using Array?
  2. Array Representation of Queue
  3. Basic Operations of Queue Using Array
  4. Example of Queue Using Array
  5. State of the Queue After Operations
  6. Why Use an Array to Implement a Queue?
  7. Implementation of Queue Using Array
  8. Applications of Queue Using Array
  9. Advantages of Queue Using Array
  10. Disadvantages of Queue Using Array
  11. Conclusion
  12. Frequently Asked Questions

What is Queue Using Array?

Queues are linear data structures that follow the First In First Out (FIFO) principle. As a result, the element that is inserted first will be removed first. A queue has two primary operations:

The queue is typically represented by two variables:

Array Representation of Queue

In an array-based implementation:

Basic Operations of Queue Using Array

The basic operations of a queue using an array are:

Enqueue Operation (Insertion)

The enqueue operation performs the following:

Algorithm for Enqueue Operation:

1. Check if the queue is full (rear == size of the array).
2. If the queue is full, print "Queue is full" and exit.
3. If the queue is not full:
   a. Increment the rear index.
   b. Insert the new element at the rear index in the array.

Dequeue Operation (Deletion)

The dequeue operation performs the following:

Algorithm for Dequeue Operation:

1. Check if the queue is empty (front == rear).
2. If the queue is empty, print "Queue is empty" and exit.
3. If the queue is not empty:
   a. Get the value at the front index of the array.
   b. Increment the front index.

Example of Queue Using Array

Let's use an array of size 5 to demonstrate the queue operations with an example.

Initial Setup

We start with two variables, front and rear both initialized to -1. This indicates that the queue is empty initially.

Operation 1: Enqueue(10)

Before Operation: front = -1, rear = -1

We insert the value 10 at the rear of the queue. Since the queue is empty, both front and rear will point to index 0.

After Operation:

Operation 2: Enqueue(20)

Before Operation: front = 0, rear = 0

We insert the value 20 at the rear of the queue. The rear is incremented to 1, and the value 20 is added at position 1.

After Operation:

Operation 3: Dequeue()

Before Operation: front = 0, rear = 1

We remove the value at the front of the queue, which is 10 (the value at index 0). After removing this element, the front is incremented to 1.

After Operation:

Operation 4: Enqueue(30)

Before Operation: front = 1, rear = 1

We insert the value 30 at the rear of the queue. The rear is incremented to 2, and the value 30 is added at position 2.

After Operation:

Operation 5: Enqueue(40)

Before Operation: front = 1, rear = 2

We insert the value 40 at the rear of the queue. The rear is incremented to 3, and the value 40 is added at position 3.

After Operation:

Operation 6: Dequeue()

Before Operation: front = 1, rear = 3

We remove the value at the front of the queue, which is 20 (the value at index 1). After removing this element, the front is incremented to 2.

After Operation:

State of the Queue After Operations

The following table summarizes the state of the queue after each operation:

Operation Front Rear Queue Array
Initial -1 -1 []
Enqueue(10) 0 0 [10]
Enqueue(20) 0 1 [10, 20]
Dequeue() 1 1 [20]
Enqueue(30) 1 2 [20, 30]
Enqueue(40) 1 3 [20, 30, 40]
Dequeue() 2 3 [30, 40]

Why Use an Array to Implement a Queue?

Using an array to implement a queue has several advantages. Arrays are contiguous blocks of memory, which allows for easy access to elements via an index. This makes it easy to manage the front and rear of the queue efficiently. The main reasons to use an array for a queue implementation are:

Implementation of Queue Using Array

Here is the implementation of the queue using array in C, C++, and Java languages:

C Program for Queue Using Array

Below is a simple C program to implement a queue using an array.

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

#define MAX 5

struct Queue {
    int items[MAX];
    int front, rear;
};

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

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

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

void enqueue(struct Queue* q, int value) {
    if (isFull(q)) {
        printf("Queue is full\n");
        return;
    }
    if (q->front == -1) {
        q->front = 0;
    }
    q->rear++;
    q->items[q->rear] = value;
    printf("Enqueued: %d\n", value);
}

int dequeue(struct Queue* q) {
    if (isEmpty(q)) {
        printf("Queue is empty\n");
        return -1;
    }
    int item = q->items[q->front];
    if (q->front == q->rear) {
        q->front = q->rear = -1;
    } else {
        q->front++;
    }
    return item;
}

int main() {
    struct Queue q;
    initQueue(&q);
    enqueue(&q, 10);
    enqueue(&q, 20);
    enqueue(&q, 30);
    printf("Dequeued: %d\n", dequeue(&q));
    return 0;
}

Queue Using an Array in C++

#include <iostream>
using namespace std;

#define MAX 5

class Queue {
private:
    int arr[MAX];
    int front, rear;

public:
    Queue() {
        front = -1;
        rear = -1;
    }

    bool isFull() {
        return rear == MAX - 1;
    }

    bool isEmpty() {
        return front == -1;
    }

    void enqueue(int value) {
        if (isFull()) {
            cout << "Queue is full!" << endl;
            return;
        }
        if (front == -1) front = 0;
        rear++;
        arr[rear] = value;
        cout << "Enqueued: " << value << endl;
    }

    int dequeue() {
        if (isEmpty()) {
            cout << "Queue is empty!" << endl;
            return -1;
        }
        int dequeuedValue = arr[front];
        if (front == rear) {
            front = rear = -1;
        } else {
            front++;
        }
        return dequeuedValue;
    }
};

int main() {
    Queue q;
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    cout << "Dequeued: " << q.dequeue() << endl;
    return 0;
}

Queue Using an Array in Java

public class Queue {
    private int[] arr;
    private int front, rear;
    private int maxSize;

    public Queue(int size) {
        arr = new int[size];
        front = -1;
        rear = -1;
        maxSize = size;
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    public boolean isEmpty() {
        return front == -1;
    }

    public void enqueue(int value) {
        if (isFull()) {
            System.out.println("Queue is full!");
            return;
        }
        if (front == -1) front = 0;
        rear++;
        arr[rear] = value;
        System.out.println("Enqueued: " + value);
    }

    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty!");
            return -1;
        }
        int dequeuedValue = arr[front];
        if (front == rear) {
            front = rear = -1;
        } else {
            front++;
        }
        return dequeuedValue;
    }

    public static void main(String[] args) {
        Queue q = new Queue(5);
        q.enqueue(10);
        q.enqueue(20);
        q.enqueue(30);
        System.out.println("Dequeued: " + q.dequeue());
    }
}

Applications of Queue Using Array

Here are the key applications of queue using an array:

Advantages of Queue Using Array

Here are some of the advantages of queue using array:

Disadvantages of Queue Using Array

Here are the disadvantages of queue using an array:

Conclusion

In conclusion, implementing a queue using an array is a simple and powerful method for managing data in a FIFO order. It is suitable for use cases where the maximum size of the queue is known and remains constant. However, for dynamic scenarios, more advanced structures like linked lists or dynamic arrays may be better suited.

Frequently Asked Questions

Is the queue size fixed when using an array?

Yes, the queue size is fixed when using an array because the array is statically allocated with a predefined maximum size.

Can I implement a priority queue using an array?

Yes, a priority queue can be implemented using an array in programming languages like Java and C++, where elements are inserted in such a way that the highest (or lowest) priority element is always at the front.

Related Articles

The following related articles are available on the NxtWave CCBP Blog:

About NxtWave

This article is published by NxtWave, an educational technology organization providing industry-recognized certifications and training programs. For more information, visit www.ccbp.in.

Contact Information:

Course Offerings: