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

implementation of queue

This document contains a C program that implements a queue data structure with basic operations such as enqueue, dequeue, and display. It defines a structure for the queue and provides functions to check if the queue is full or empty. The main function presents a menu for user interaction to perform various queue operations.

Uploaded by

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

implementation of queue

This document contains a C program that implements a queue data structure with basic operations such as enqueue, dequeue, and display. It defines a structure for the queue and provides functions to check if the queue is full or empty. The main function presents a menu for user interaction to perform various queue operations.

Uploaded by

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

#include <stdio.

h>

#include <stdlib.h>

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

// Declare a structure for the queue

struct Queue {

int arr[MAX];

int front;

int rear;

};

// Initialize the queue

void initQueue(struct Queue* q) {

q->front = -1;

q->rear = -1;

// Function to check if the queue is full

int isFull(struct Queue* q) {

return q->rear == MAX - 1;

// Function to check if the queue is empty

int isEmpty(struct Queue* q) {

return q->front == -1 || q->front > q->rear;

// Function to enqueue an element into the queue

void enqueue(struct Queue* q, int value) {

if (isFull(q)) {

printf("Queue is full! Cannot enqueue %d.\n", value);

} else {

if (q->front == -1) {

q->front = 0; // If the queue is empty, set front to 0

q->rear++;
q->arr[q->rear] = value; // Insert the element

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

// Function to dequeue an element from the queue

int dequeue(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty! Cannot dequeue.\n");

return -1; // Indicates an error (queue is empty)

} else {

int value = q->arr[q->front];

q->front++;

return value;

// Function to display the elements in the queue

void display(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty.\n");

} else {

printf("Queue elements: ");

for (int i = q->front; i <= q->rear; i++) {

printf("%d ", q->arr[i]);

printf("\n");

int main() {

struct Queue q;

initQueue(&q); // Initialize the queue

int choice, value;


do {

printf("\nQueue Operations Menu:\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. Check if Full\n");

printf("5. Check if Empty\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: // Enqueue

printf("Enter value to enqueue: ");

scanf("%d", &value);

enqueue(&q, value);

break;

case 2: // Dequeue

value = dequeue(&q);

if (value != -1) {

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

break;

case 3: // Display

display(&q);

break;

case 4: // Check if Full

if (isFull(&q)) {

printf("Queue is full.\n");

} else {

printf("Queue is not full.\n");

}
break;

case 5: // Check if Empty

if (isEmpty(&q)) {

printf("Queue is empty.\n");

} else {

printf("Queue is not empty.\n");

break;

case 6: // Exit

printf("Exiting...\n");

break;

default:

printf("Invalid choice! Please try again.\n");

} while (choice != 6); // Continue until the user chooses to exit

return 0;

You might also like