Expt4 DS
Expt4 DS
Date of Performance:
Date of Submission :
THEORY:
Queues can be easily represented using linear arrays. every queue has front and rear variables that
point to the position from where deletions and insertions can be done, respectively. Check if the
queue is already full by comparing rear to max - 1. if so, then return an overflow error. If the item
is to be inserted as the first element in the list, in that case set the value of front and rear to 0 and
insert the element at the rear end. Otherwise keep increasing the value of rear and insert each
element one by one having rear as the index. If we want to delete an element from the queue, then
the value of FRONT will be incremented. Deletions are done from only this end of the queue.
Before deleting an element from a queue, we must check for underflow conditions. An underflow
condition occurs when we try to delete an element from a queue that is already empty. If FRONT
= –1 and REAR = –1, it means there is no element in the queue.
ALGORITHMS:
FRONT = 0 and REAR = 5. Suppose we want to add another element with value 45, then REAR
would be incremented by 1 and the value would be stored at the position pointed by REAR. Now,
FRONT = 0 and REAR = 6. Every time a new element has to be added, we repeat the same
procedure.
PROGRAM:
SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
OUTPUT:
Conclusion / Outcome:
Queue is a data structure that works on the principle of First In First Out(FIFO).
Enqueue operation is performed to insert the element at the rear end of the queue.
Dequeue operation is performed to delete elements from the front end of the queue.
Display operation traverse the queue and print all its elements.
Front will return the front element of the queue if the queue is not empty.