Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Practical No-8

Aim: Perform insert and delete operations on linear queue using


array
Perform insert and delete operations on linear queue using array

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5;
int q[SIZE];
int f=-1;
int r=-1;
void insert(int);
void deletion(int);
void display_queue();
int main()
{
int ch,x;
clrscr();
do
{
printf("\nQueue Operations:\n1.Insert an element\n2.Delete an
element\n3.elements\n4.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter value to be added:\n");
scanf("%d",&x);
insert(x);
break;

case 2:deletion(x);
break;

case 3:display_queue();
break;

case 4:exit(0);
break;

default:printf("Enter valid choice \n");


}
}while(ch!=4);
getch();
return 0;
}
void insert(int x)
{
if(r==SIZE-1)
{
printf("Queue Overflow");
}
else
{
if((f==-1&&r==-1)||(f>r))
{
f=0;
r=0;
}
else
{
r=r+1;
}
q[r]=x;
}
}
void deletion(int x)
{
if((f==-1&&r==-1)||(f>r))
{
printf("Queue underflow\n");
}
else
{
x=q[f];
printf("%d is deleted\n",x);
f=f+1;
}
}
void display_queue()
{
int i;
if((f==-1&&r==-1)||(f>r))
{
printf("Queue is Empty");
}
else
{
for(i=f;i<=r;i++)
{
printf("%d\t",q[i]);
}
}
}

OUTPUT:

You might also like