Experiment 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Experiment No.

4
Circular Queue

Implement Circular Queue ADT using an Array.

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

// Circular Queue structure


struct CircularQueue {
int *items;
int front;
int rear;
int size;
};

// Initialize queue with a given size


void initialize(struct CircularQueue* q, int size) {
q->size = size;
q->items = (int *)malloc(size * sizeof(int));
q->front = -1;
q->rear = -1;
}

// Check if the queue is full


int isFull(struct CircularQueue* q) {
return ((q->rear + 1) % q->size == q->front);
}

// Check if the queue is empty


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

// Add an element to the queue


void enqueue(struct CircularQueue* q, int value) {
if (isFull(q)) {
printf("Queue is full! Cannot enqueue %d\n", value);
} else {
if (q->front == -1) // If the queue is initially empty
q->front = 0;

q->rear = (q->rear + 1) % q->size; // Move rear in a circular manner


q->items[q->rear] = value;
printf("Enqueued %d\n", value);
}
}

// Remove an element from the queue


int dequeue(struct CircularQueue* q) {
int element;
if (isEmpty(q)) {
printf("Queue is empty! Cannot dequeue\n");
return -1;
} else {
element = q->items[q->front];
if (q->front == q->rear) { // Queue has only one element, so reset it
q->front = -1;
q->rear = -1;
} else {
q->front = (q->front + 1) % q->size; // Move front in a circular manner
}
printf("Dequeued %d\n", element);
return element;
}
}

// Display the elements of the queue


void display(struct CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
} else {
printf("Queue elements are: ");
int i = q->front;
while (1) {
printf("%d ", q->items[i]);
if (i == q->rear)
break;
i = (i + 1) % q->size;
}
printf("\n");
}
}

// Main function to interactively test the circular queue


int main() {
struct CircularQueue q;
int n, choice, value;

printf("Enter the maximum number of elements for the circular queue: ");
scanf("%d", &n);

initialize(&q, n);

// Allow user to add initial values up to the defined size


printf("Enter up to %d initial values (or enter -1 to stop):\n", n);
for (int i = 0; i < n; i++) {
printf("Enter value %d: ", i + 1);
scanf("%d", &value);
if (value == -1) {
break;
}
enqueue(&q, value);
}

while (1) {
printf("\nCircular Queue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
display(&q);
break;

case 2:
dequeue(&q);
display(&q);
break;

case 3:
display(&q);
break;

case 4:
printf("Exiting...\n");
free(q.items); // Free dynamically allocated memory
return 0;

default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}
OUTPUT
Enter the maximum number of elements for the circular queue: 6
Enter up to 6 initial values (or enter -1 to stop):
Enter value 1: 45
Enqueued 45
Enter value 2: 12
Enqueued 12
Enter value 3: 36
Enqueued 36
Enter value 4: 56
Enqueued 56
Enter value 5: 5
Enqueued 5
Enter value 6: -1

Circular Queue Menu:


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4 3
Queue elements are: 45 12 36 56 5

Circular Queue Menu:


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 78
Enqueued 78
Queue elements are: 45 12 36 56 5 78

Circular Queue Menu:


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued 45
Queue elements are: 12 36 56 5 78

Circular Queue Menu:


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements are: 12 36 56 5 78

Circular Queue Menu:


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...

You might also like