9842 DSA Exp2

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

FR.

CONCEICAO RODRIGUES COLLEGE OF


ENGINEERING
Department of Electronics and Computer Science
Class: S.E. ECS(Semester III) Subject Name: Data Structures and Algorithms
(ECC 303)

Name of the Student: Wasee Momin

Experiment No 4 Roll Number 9842


Date of 4-9-23 Date of Submission 18-9-23
Performance
Experiment Title Static Implementation of queue
CO Mapping ECC303.2

Problem Statement:
Write C Program to implement queue using arrays.

Objective of the Experiment:


Understand basic operations like enqueue and dequeue in 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

Enqueue second 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;
}

int enqueue(int queue[])


{
int data;
if(isFull())
{
printf("\n Queue overflow");
}
else
{
printf("\n Enter data");
scanf("%d",&data);
if(rear==-1 && front==-1)
{
rear++;
queue[rear]=data;
front++;
}
else if(rear<MAX-1 && rear!=front)
{
queue[rear]=data;
rear++;
}
else if(rear=MAX-1 && front!=0)
{
rear=0;
queue[rear]=data;
rear++;
}
}
return rear;
}

int dequeue(int queue[])


{
if(isEmpty())
{
printf("\n Queue underflow");
}
else
{
printf("\n Element deleted =%d",queue[front]);
if(front<MAX-1 && front!=rear)
{
front++;
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science
}
else if(front==MAX-1 && front!=rear)
{
front==0;
}
else
{
rear==front==-1;
}
}
return front;
}

void peek(int Queue[])


{
if(isEmpty())
{
printf("\n Queue underflow");
}
else
{
printf("\n Element at the front of Queue=%d",Queue[front]);
}
}

void display(int queue[])


{
int i;
if(isEmpty())
{
printf("\n Queue underflow");
}
else
{
for(i=front;i<=rear;i++)
printf("\n %d",queue[i]);
}
}
`

OUTPUT:
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science

The program was tested for different sets of inputs.


Program is working is SATISFACTORY NOT SATISFACTORY (Tick appropriate
outcome)
Post Lab Questions:
1. What are the applications of queue?
2. Write a program to reverse the elements of the queue.
Evaluation: -
On Time Completion Knowledge of Implementation and Total (10)
and Submission (2) the topic (4) Output (4)

Date and Signature of teacher:

You might also like