Prg_5
Prg_5
QUEUE OPERATIONS
5. Write a C Program to perform QUEUE operations using Arrays
AIM :
To write a C Program to perform Queue operations using Arrays
ALGORITHM :
Insert()
Step 1: Start
Step 2: If front = NULL
front = rear = 0
queue[front]=element
Stop
Step 3: if rear = MAX-1
Display the message, “Queue is Full” and
Stop
Step 4: rear = rear +1
Step 5: queue[rear] = element
Step 6: Stop
Delete()
Step 1: Start
Step 2: if front = NULL and rear = NULL
Display the message, “Queue is Empty”
And Stop
Step 3: if front = rear
Set i = queue[front]
Set front = rear = NULL
Return the deleted element i and
Stop
Step 4: Set i = queue[front]
SteP 5: front++;
Step 5: Return the deleted element i
Step 6: Stop
PROGRAM :
#include <stdio.h>
#include<conio.h>
void insert();
void delete();
void display();
int queue_array[100],x,i,choice,n;
int rear = - 1;
int front = - 1;
void main()
{
clrscr();
printf("\n Enter the size of QUEUE[MAX=100]:");
scanf("%d", &n);
do
{
clrscr();
printf("\n\n\t\t\tQUEUE OPERATION USING ARRAY");
printf("\n\t\t\t----------------------------");
printf("\n\n\t\t\t 1.INSERT");
printf("\n\t\t\t 2.DELETE");
printf("\n\t\t\t 3.DISPLAY");
printf("\n\t\t\t 4.EXIT");
printf("\n\t\t\tENTER CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: printf("\n\t\t\tEXIT POINT");
break;
default: printf("\n\t\t\tINVALID CHOICE");
}
} while(choice!=4);
}
void insert()
{
if (rear >= n - 1)
{
printf("\n\t\t\tQUEUE OVERFLOW\n");
}
else
{
if (front == - 1)
{
front = 0;
}
printf("\n\t\t\tENTER ELEMENT TO BE INSERTED : ");
scanf("%d", &x);
rear = rear + 1;
queue_array[rear] = x;
printf("\n\t\t\tELEMENT INSERTED…");
getch();
}
}
void delete()
{
if (front <= - 1 || front > rear)
{
printf("\n\t\t\tQUEUE UNDERFLOW");
return ;
}
else
{
printf("\n\t\t\tDELETED ELEMENT : %d",queue_array[front]);
front = front + 1;
}
getch();
}
void display()
{
int i;
if (front >= 0)
{
printf("\n\t\t\tELEMENTS IN QUEUE : \n");
for(i=front; i<=rear; i++)
{
printf("\n\t\t\t%d ",queue_array[i]);
}
}
else
{
printf("\n\t\t\tQUEUE IS EMPTY");
}
getch();
}
OUTPUT :