Implementation Of Insert, Delete Algorithms of Queue using Array

Problem: Insert, Delete Algorithms of Queue using Array


#include<stdio.h>
#include<conio.h>
#define max 5

int q[max];
int front=-1,rear=-1;

void insert()
{
if(rear == max)
{
printf("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;
printf("\nEnter the value:");
scanf("%d",&q[rear]);
}
}

void del()
{
if(front == -1 || front>=rear)
printf("Queue is empty");
else
{
printf("\n%d is deleted value",q[front]);
front=front+1;
}
}

void display()
{
int i;
if(front == -1)
printf("Queue is empty!!");
else
{
for(i=front;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