Implementation Of Insert, Delete Algorithms of Circular Queue In C / Data Structure / Linear Data Structure
Problem: Insert, Delete Algorithms of Circular Queue
#include<stdio.h>
#include<conio.h>
#define max 5
int q[max];
int front=-1,rear=-1;
void insert()
{
if(front ==0 && rear == max-1 || front==rear+1)
{
printf("Circular Queue is full!!!");
}
else if(front==-1 && rear==-1)
{
front=front+1;
printf("\nEnter the value:");
scanf("%d",&q[front]);
rear=rear+1;
rear=front;
}
else
{
rear=(rear+1)%max;
printf("\nEnter the value:");
scanf("%d",&q[rear]);
}
}
void del()
{
if(front == -1)
printf("Queue is empty");
else if(front == rear)
{
printf("\n%d is deleted value",q[front]);
front=-1;
rear=-1;
}
else
{
printf("\n%d is deleted value",q[front]);
front=(front+1)%max;
}
}
void display()
{
int i;
if(front == -1 && rear==-1)
printf("Queue is empty!!");
else
{
if(front > rear)
{
for(i=front;i<max;i++)
{
printf("%d",q[i]);
}
for(i=0;i<=rear;i++)
{
printf("%d",q[i]);
}
}
else
{
for(i=(front%max);i<=rear;i++)
{
printf("%d\t",q[i]);
}
}
}
}
void main()
{
int n;
clrscr();
while(1)
{
printf("\n***** QUEUE OPERATIONS *****");
printf("\n1. INSERT");
printf("\n2. DELETE");
printf("\n3. DISPLAY");
printf("\n4. EXIT");
printf("\nenter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("Wrong Choice!!");
}
}
}
Comments