Monday, 14 October 2024

Write a program of circular queue.

 #include <stdio.h>

#define SIZE 5


int cqueue[SIZE], front = -1, rear = -1;


void enqueue(int value) {

    if ((rear + 1) % SIZE == front)

        printf("Circular Queue is full!\n");

    else {

        if (front == -1)

            front = 0;

        rear = (rear + 1) % SIZE;

        cqueue[rear] = value;

        printf("Inserted %d\n", value);

    }

}


void dequeue() {

    if (front == -1)

        printf("Circular Queue is empty!\n");

    else {

printf("Deleted %d\n", cqueue[front]);

        if (front == rear) {

            front = -1;

            rear = -1;

        } else {

            front = (front + 1) % SIZE;

        }

    }

}





void display() {

    if (front == -1)

        printf("Circular Queue is empty!\n");

    else {

        printf("Circular Queue elements are: ");

 for (int i = front; i != rear; i = (i + 1) % SIZE) 

{

  printf("%d ", cqueue[i]);

  }

  printf("%d\n", cqueue[rear]);

    }

}



int main() {

    enqueue(10);

    enqueue(20);

    enqueue(30);

    dequeue();

    display();

    return 0;

}