9842 DSA Exp2
9842 DSA Exp2
9842 DSA Exp2
Problem Statement:
Write C Program to implement queue using arrays.
Theory:
A queue is a linear data structure in which insertion is done at one end,
whereas deletion is performed at the other end.
Addition of an element in the queue can be done from the end called Rear
and the element can be removed from the other end called Front. Since
the element that is inserted first would be the element to be taken out first,
a queue is also called First In First Out (FIFO) structure
The fundamental operations on a queue are:
Enqueue - insert an element at the end of the list(Rear).
Dequeue - delete (and return) the element at the start of the
list(Front).
Peek – display an element at the Front.
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science
Every queue has three variables front and rear that point to the position
from where deletions and insertions can be done, respectively. Third
variable, Max is used to store the maximum number of elements that the
queue can store.
When Rear = Max-1, the queue is full. If Front = Rear = Null, the queue is
empty.
Enqueue Operation: -
Enqueue operation increments the Rear pointer and inserts an element in
the queue where Rear is currently pointing. However, before inserting an
element in a queue, we must check for overflow condition i.e. to check
whether we are trying to insert an element into a queue that is already full.
The overflow condition for queue is given by Rear = Max – 1.
Algorithm:
1: IF Rear = Max-1
Write OVERFLOW
Goto 4
2: IF Front = -1 and Rear = -1 (Check if queue is empty)
SET Front = Rear = 0
ELSE SET Rear = Rear+1
3: SET QUEUE[Rear] = Value
4: EXIT
Example:
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science
Enqueue first element
Dequeue Operation:
Dequeue operation deletes and returns the value where Front is pointing.
However, before deleting an element from a queue, we must check for
underflow conditions i.e. are we trying to delete an element from a queue
that is already empty? If Front = –1 and Rear = –1, it means that the
queue is empty, there is no element in the queue.
Algorithm:
1: IF Front = -1 OR Front > Rear
Write UNDERFLOW
ELSE
SET Val = QUEUE[Front]
SET Front = Front+1
2: EXIT
Example:
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science
Source Code:
/* Name:Wasee Momin
Roll No:9842
Title: Exp 2- Static implementation of Queue.
*/
#include<stdio.h>
#define MAX 20
int front=-1,rear=-1;
int enqueue(int []);
int dequeue(int []);
int isFull();
int isEmpty();
void peek(int []);
void display(int []);
void main()
{
int queue[MAX],n=1,option;
do
{
printf("\n Enter \n1:Enqueue \n2:Dequeue \n3:Peek \n4:Display");
scanf("%d",&option);
switch(option)
{
case 1:
rear=enqueue(queue);
break;
case 2:
front=dequeue(queue);
break;
case 3:
peek(queue);
break;
case 4:
display(queue);
break;
defaultcase:
printf("\n Invalid option");
break;
}//End of switch case
printf("\n Enter 0 to exit");
scanf("%d",&n);
}while(n!=0);
}
int isEmpty()
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science
{
if(rear==-1 && front==-1)
return 1;
else
return 0;
}
int isFull()
{
if(front>=MAX-1 && front>rear)
return 1;
else
return 0;
}
OUTPUT:
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science