DS LAB BSC(cs) 2nd SEM 2025

Queue Implementation Using Array


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

#define MAX 5 // maximum size of the queue

// Define a Queue structure
struct Queue {
    int front, rear;
    int arr[MAX];
};

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

// Check if the queue is empty
int isEmpty(struct Queue* q) {
    return (q->front == -1);
}

// Check if the queue is full
int isFull(struct Queue* q) {
    return (q->rear == MAX - 1);
}

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

// Dequeue operation (remove an element from the queue)
int dequeue(struct Queue* q) {
    if (isEmpty(q)) {
        printf("Queue is empty! Cannot dequeue.\n");
        return -1; // Return -1 to indicate an error
    } else {
        int dequeuedValue = q->arr[q->front];
        if (q->front == q->rear) { // If the queue will be empty after dequeue
            q->front = q->rear = -1;
        } else {
            q->front++;
        }
        return dequeuedValue;
    }
}

// Display elements in the queue
void display(struct Queue* q) {
    if (isEmpty(q)) {
        printf("Queue is empty!\n");
    } else {
        printf("Queue elements: ");
        for (int i = q->front; i <= q->rear; i++) {
            printf("%d ", q->arr[i]);
        }
        printf("\n");
    }
}

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

    enqueue(&q, 10);
    enqueue(&q, 20);
    enqueue(&q, 30);
    enqueue(&q, 40);
    enqueue(&q, 50);

    display(&q);

    int dequeuedValue = dequeue(&q);
    if (dequeuedValue != -1) {
        printf("Dequeued: %d\n", dequeuedValue);
    }

    display(&q);

    enqueue(&q, 60);
    display(&q);

    return 0;
}
OUTPUT

Enqueued: 10 Enqueued: 20 Enqueued: 30 Enqueued: 40 Enqueued: 50 Queue elements: 10 20 30 40 50 Dequeued: 10 Queue elements: 20 30 40 50 Queue is full! Cannot enqueue 60 Queue elements: 20 30 40 50


Algorithm for Queue Implementation Using Array


  1. Initialize the Queue
    • Set front = -1 and rear = -1 to indicate an empty queue.
  2. Check if the Queue is Empty
    • If front == -1, return true (queue is empty).
    • Otherwise, return false.
  3. Check if the Queue is Full
    • If rear == MAX - 1, return true (queue is full).
    • Otherwise, return false.
  4. Enqueue (Insert an Element into the Queue)
    • If the queue is full, print an error message.
    • If the queue is empty (front == -1), set front = 0.
    • Increment rear and insert the new element at arr[rear].
  5. Dequeue (Remove an Element from the Queue)
    • If the queue is empty, print an error message and return -1.
    • Store arr[front] in a variable.
    • If front == rear, reset front and rear to -1 (queue becomes empty).
    • Otherwise, increment front to remove the front element.
    • Return the dequeued element.
  6. Display the Queue Elements
    • If the queue is empty, print an empty message.
    • Otherwise, loop from front to rear and print each element.
  7. Main Function Execution
    • Initialize the queue.
    • Perform enqueue operations.
    • Display the queue.
    • Perform a dequeue operation and display the updated queue.
    • Enqueue another element and display the queue again.

 

Comments

Popular posts from this blog

C-Programming of Class 12

C-Program of Class 11