0% found this document useful (0 votes)
11 views

circular queue using array

The document contains a C program that implements a circular queue with a maximum size of 5. It includes functions for checking if the queue is full or empty, enqueuing and dequeuing elements, retrieving the front element, and displaying the queue. The main function demonstrates the usage of these operations, including handling overflow and underflow conditions.

Uploaded by

chandrak1302
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

circular queue using array

The document contains a C program that implements a circular queue with a maximum size of 5. It includes functions for checking if the queue is full or empty, enqueuing and dequeuing elements, retrieving the front element, and displaying the queue. The main function demonstrates the usage of these operations, including handling overflow and underflow conditions.

Uploaded by

chandrak1302
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

#include <stdlib.h>

#define SIZE 5 // Define maximum size of Queue

int queue[SIZE]; // Array to store queue elements

int front = -1, rear = -1; // Front and Rear pointers

// Function to check if queue is full

int isFull() {

return (rear + 1) % SIZE == front;

// Function to check if queue is empty

int isEmpty() {

return front == -1;

// Function to enqueue an element

void enqueue(int value) {

if (isFull()) {

printf("Queue Overflow! Cannot enqueue %d\n", value);

return;

if (isEmpty()) {

front = rear = 0; // First element

} else {

rear = (rear + 1) % SIZE;

queue[rear] = value;

printf("Enqueued: %d\n", value);

// Function to dequeue an element

int dequeue() {

if (isEmpty()) {

printf("Queue Underflow! No elements to dequeue\n");


return -1;

int removed = queue[front];

if (front == rear) { // Only one element left

front = rear = -1;

} else {

front = (front + 1) % SIZE;

return removed;

// Function to get the front element

int getFront() {

if (isEmpty()) {

printf("Queue is Empty!\n");

return -1;

return queue[front];

// Function to display the queue

void display() {

if (isEmpty()) {

printf("Queue is Empty!\n");

return;

printf("Queue: ");

int i = front;

while (1) {

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

if (i == rear) break;

i = (i + 1) % SIZE;

}
printf("\n");

// Main function

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

enqueue(40);

enqueue(50);

enqueue(60); // This should show an overflow error

display();

printf("Dequeued: %d\n", dequeue());

printf("Dequeued: %d\n", dequeue());

display();

enqueue(60);

enqueue(70);

display();

printf("Front Element: %d\n", getFront());

return 0;

You might also like