Steps for Implementation:

  1. Queue Representation:
    • We need an array to hold the queue elements.
    • Two pointers, front and rear, will indicate the start and end of the queue.
  2. Enqueue Operation:
    • We add an element at the rear of the queue.
    • If the queue is full, we can’t add any more elements.
    • If the queue isn’t full, we move the rear pointer forward in a circular manner (using modulo %).
  3. Dequeue Operation:
    • We remove an element from the front of the queue.
    • If the queue is empty, we can’t remove any elements.
    • After dequeuing, we move the front pointer forward in a circular manner.
  4. Check for Empty and Full Conditions:
    • A queue is empty if front == -1.
    • A queue is full if (rear + 1) % maxSize == front.
  5. Display Operation:
    • To display the queue, we need to traverse it starting from front to rear considering the circular nature.

Program in C

#include <stdio.h>
#define MAX 5 // Define the maximum size of the circular queue

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

// Function to initialize the queue
void initialize Queue(struct Circular Queue *q) 
{
    q->front = -1;
    q->rear = -1;
}

// Function to check if the queue is full
int is Full(struct Circular Queue *q) 
{
    if ((q->rear + 1) % MAX == q->front) 
{
        return 1;
    }
    return 0;
}

// Function to check if the queue is empty
int is Empty(struct Circular Queue *q) 
{
    if (q->front == -1) 
{
        return 1;
    }
    return 0;
}

// Function to add an element to the circular queue
void enqueue(struct Circular Queue *q, int value) 
{
    if (is Full(q)) 
{
        printf("Queue is Full!\n");
    } 
else 
{
        if (q->front == -1) // Inserting the first element
            q->front = 0;
        q->rear = (q->rear + 1) % MAX;
        q->items[q->rear] = value;
        printf("Inserted %d\n", value);
    }
}

// Function to remove an element from the circular queue
int dequeue(struct Circular Queue *q) 
{
    int element;
    if (is Empty(q)) 
{
        printf("Queue is Empty!\n");
        return -1;
    } 
else 
{
        element = q->items[q->front];
        if (q->front == q->rear) 
{ // Queue has only one element
            q->front = -1;
            q->rear = -1;
        } 
else 
{
            q->front = (q->front + 1) % MAX;
        }
        printf("Deleted %d\n", element);
        return element;
    }
}

// Function to display the circular queue
void display(struct Circular Queue *q) 
{
    int i;
    if (is Empty(q)) 
{
        printf("Queue is Empty!\n");
    } 
else 
{
        printf("Front -> %d\n", q->front);
        printf("Items -> ");
        for (i = q->front; i != q->rear; i = (i + 1) % MAX) 
{
            printf("%d ", q->items[i]);
        }
        printf("%d ", q->items[i]);
        printf("\nRear -> %d\n", q->rear);
    }
}

// Main function
int main() 
{
    struct Circular Queue q;
    initialize Queue(&q);

    // Test enqueue operation
    enqueue(&q, 10);
    enqueue(&q, 20);
    enqueue(&q, 30);
    enqueue(&q, 40);
    enqueue(&q, 50); // Queue is full now

    // Test display operation
    display(&q);

    // Test dequeue operation
    dequeue(&q);
    dequeue(&q);

    // Display after two dequeue operations
    display(&q);

    // Enqueue after dequeue operations
    enqueue(&q, 60);
    display(&q);

    return 0;
}

Explanation:

  1. initializeQueue():
    • Initializes the queue by setting both front and rear to -1, which indicates an empty queue.
  2. isFull():
    • Checks if the queue is full by checking if (rear + 1) % MAX == front. This means that if the position just after rear is front, the queue is full.
  3. isEmpty():
    • Checks if the queue is empty by verifying if front == -1.
  4. enqueue():
    • Adds an element to the rear of the queue.
    • If the queue is empty, we set front to 0 because we are inserting the first element.
    • Then, we update the rear to the next position using (rear + 1) % MAX.
    • Finally, the value is inserted at the rear position.
  5. dequeue():
    • Removes the front element from the queue.
    • If the queue becomes empty after dequeuing (i.e., front == rear), we reset both pointers to -1.
  6. display():
    • Prints the queue from the front to the rear, accounting for the circular nature by looping through indices using modulo.

Sample Output:

Inserted 10
Inserted 20
Inserted 30
Inserted 40
Inserted 50
Queue is Full!
Front -> 0
Items -> 10 20 30 40 50 
Rear -> 4
Deleted 10
Deleted 20
Front -> 2
Items -> 30 40 50 
Rear -> 4
Inserted 60
Front -> 2
Items -> 30 40 50 60 
Rear -> 0

Key Points:

  • Circular queues help in efficient use of space by reusing positions in the array once elements are dequeued.
  • The implementation uses simple operations like modulo arithmetic to wrap around the array when needed.

Leave a Reply

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

Verified by MonsterInsights