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

Queue Menu Dynamic

This document presents a C program for a menu-driven queue implementation with user-defined size. It includes functions for enqueueing, dequeueing, and displaying queue elements, along with error handling for overflow and underflow conditions. The program dynamically allocates memory for the queue and provides a user interface for interaction.

Uploaded by

rehan107968
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)
2 views2 pages

Queue Menu Dynamic

This document presents a C program for a menu-driven queue implementation with user-defined size. It includes functions for enqueueing, dequeueing, and displaying queue elements, along with error handling for overflow and underflow conditions. The program dynamically allocates memory for the queue and provides a user interface for interaction.

Uploaded by

rehan107968
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

Queue Implementation in C (Menu-driven with

User-defined Size)
#include <stdio.h>
#include <stdlib.h>

int *queue;
int front = -1, rear = -1, SIZE;

// Function to insert (enqueue) an element


void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue Overflow! Cannot insert %d\n", value);
} else {
if (front == -1) front = 0; // set front to 0 on first insertion
rear++;
queue[rear] = value;
printf("%d inserted into queue.\n", value);
}
}

// Function to remove (dequeue) an element


void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow! Cannot remove element.\n");
} else {
printf("%d removed from queue.\n", queue[front]);
front++;
}
}

// Function to display the queue


void display() {
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
int choice, value;

printf("Enter maximum size of the queue: ");


scanf("%d", &SIZE);

queue = (int *)malloc(SIZE * sizeof(int)); // dynamic allocation

while (1) {
printf("\n--- Queue Menu ---\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
free(queue); // free memory
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}

You might also like