Solution:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define maxsize 5
void insert();
void delete();
void display();
int front=-1,rear=-1;
int queue[maxsize];
void main()
{
int choice;
while(choice!=4)
{
printf("\n1. Insert an element");
printf("\n2. Delete an element");
printf("\n3. Display Queue");
printf("\n4. Exit\n");
printf("\nEnter your choice :\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("Enter valid choice\n");
}
}
}
void insert()
{
int item;
printf("\nEnter element :\n");
scanf("%d",&item);
if((rear+1)%maxsize==front)
{
printf("\nOverflow");
return ;
}
else if((front==-1)&&(rear==-1))
{
front=rear=0;
}
else if((rear==maxsize-1)&&(front!=0))
{
rear=0;
}
else
{
rear=(rear+1)%maxsize;
}
queue[rear]=item;
printf("\nElement inserted successfully\n\n");
}
void delete()
{
int item;
if((front==-1)&&(rear==-1))
{
printf("Underflow");
return ;
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
printf("Element popped successfully\n");
}
void display()
{
int i;
if(front==-1)
{
printf("\n Circular queue is empty.\n");
}
else
{
i=front;
printf("Circular queue elements are :\n");
if(front<=rear)
{
while(i<=rear)
{
printf("%d \n",queue[i]);
i++;
}
}
else
{
while(i<=maxsize-1)
{
printf("%d \n",queue[i]);
i++;
}
i=0;
while(i<=rear)
{
printf("%d \n",queue[i]);
i++;
}
}
}
}
0 Comments