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

Write A C Program To Perform The Fo

The document provides a C program that implements a queue with operations to insert, delete, and display elements. It defines a maximum size for the queue and includes functions to check if the queue is full or empty, as well as to handle the insertion and deletion of elements. The main function allows users to interact with the queue through a menu-driven interface.

Uploaded by

Swatilagna sahu
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)
20 views2 pages

Write A C Program To Perform The Fo

The document provides a C program that implements a queue with operations to insert, delete, and display elements. It defines a maximum size for the queue and includes functions to check if the queue is full or empty, as well as to handle the insertion and deletion of elements. The main function allows users to interact with the queue through a menu-driven interface.

Uploaded by

Swatilagna sahu
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

Write a C program to perform the following operations on the QUEUE: Insert, Delete

and Display.

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

#define MAX 5 // Define the maximum size of the queue

int queue[MAX];
int front = -1, rear = -1;

// Function to check if the queue is full


int isFull() {
return (rear == MAX - 1);
}

// Function to check if the queue is empty


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

// Function to insert an element into the queue


void insert(int element) {
if (isFull()) {
printf("Queue Overflow! Cannot insert the element.\n");
} else {
if (front == -1) // When inserting the first element
front = 0;
rear++;
queue[rear] = element;
printf("Element %d inserted into the queue.\n", element);
}
}

// Function to delete an element from the queue


void delete() {
if (isEmpty()) {
printf("Queue Underflow! Cannot delete any element.\n");
} else {
printf("Element %d deleted from the queue.\n", queue[front]);
front++;
if (front > rear) // Reset the queue after the last element is deleted
front = rear = -1;
}
}

// Function to display the queue elements


void display() {
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
int choice, element;

while (1) {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf("%d", &element);
insert(element);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4

You might also like