[go: up one dir, main page]

0% found this document useful (0 votes)
3 views2 pages

circular queue simplified

The document contains a C program that implements a circular queue with basic operations such as enqueue, dequeue, and display. It defines functions to check if the queue is full or empty, and manages the insertion and removal of elements while maintaining the circular structure. The main function demonstrates the functionality of the queue by adding and removing elements, including handling the case when the queue is full.

Uploaded by

meshrsth10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

circular queue simplified

The document contains a C program that implements a circular queue with basic operations such as enqueue, dequeue, and display. It defines functions to check if the queue is full or empty, and manages the insertion and removal of elements while maintaining the circular structure. The main function demonstrates the functionality of the queue by adding and removing elements, including handling the case when the queue is full.

Uploaded by

meshrsth10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#define SIZE 5

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

// Check if the queue is full


int isFull() {
return (front == (rear + 1) % SIZE);
}

// Check if the queue is empty


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

// Adding an element
void enQueue(int element) {
if (isFull()) {
printf("\nQueue is full!!\n");
return;
}
if (front == -1) front = 0; // First element being added
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\nInserted -> %d", element);
}

// Removing an element
int deQueue() {
if (isEmpty()) {
printf("\nQueue is empty!!\n");
return -1;
}
int element = items[front];
if (front == rear) {
front = rear = -1; // Reset if queue becomes empty
} else {
front = (front + 1) % SIZE;
}
printf("\nDeleted element -> %d\n", element);
return element;
}

// Display the queue


void display() {
if (isEmpty()) {
printf("\nEmpty Queue\n");
return;
}
printf("\nFront -> %d\nItems -> ", front);
for (int i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d\nRear -> %d\n", items[rear], rear);
}

int main() {
enQueue(10);
enQueue(20);
enQueue(30);
display();

deQueue();
display();

enQueue(40);
enQueue(50);
enQueue(60); // Should indicate the queue is full
display();

return 0;
}

You might also like