Write a menu driven program to implement circular queue
operations such as Enqueue, Dequeue, Peek, Display of elements,
isEmpty, isFull using dynamic array.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int* circularQueue;
int front = -1, back = -1, n=10;
void enqueue(int number){
if ((front == 0 && back == n-1) || (front == back+1)) {
printf("The size of the queue exceeded \n");
return;
}
if (front == -1) {
front = 0;
back = 0;
}
else {
if (back == n - 1)
back = 0;
else
back = back + 1;
}
circularQueue[back] = number ;
}
void dequeue(){
if (front == -1) {
printf("Queue Underflow\n");
return ;
}
printf("Element deleted is : %d ", circularQueue[front]);
if (front == back) {
front = -1;
back = -1;
}
else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void Peek(){
if (front == -1) {
printf("Queue Underflow\n");
return ;
}
printf("Element peek is : %d ", circularQueue[front]);
}
void display(){
int f = front, r = back;
if (front == -1) {
printf("No number in the queue");
return;
}
printf("Queue Contents :\n");
if (f <= r) {
while (f <= r){
printf("%d\n", circularQueue[f]);
f++;
}
}
else {
while (f <= n - 1) {
printf("%d\n",circularQueue[f]);
f++;
}
f = 0;
while (f <= r) {
printf("%d\n",circularQueue[f]);
f++;
}
}
}
int isEmpty(){
return (front == -1);
}
int isFull(){
return ((front == 0 && back == n-1) || (front == back+1));
}
int main(){
int value,choice;
int size;
printf("Enter the size of queue:");
scanf("%d",&size);
circularQueue=(int*)malloc(size * sizeof(int));
while(1){
printf("1. Add an item to the queue\n");
printf("2. Delete an item from the queue\n");
printf("3. Display the queue\n");
printf("4. Exit\n");
printf("Select an option:");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter a number: ");
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong option");
}
printf("\n\n");
}
free(circularQueue);
return 0;
}
Comments
Leave a comment