0% found this document useful (0 votes)
14 views4 pages

Expt4 DS

The document outlines an experiment to implement a Linear Queue Abstract Data Type (ADT) using an array. It includes the theory behind queues, algorithms for insertion and deletion, a source code example in C, and concludes with the principles of queue operations. The queue operates on a First In First Out (FIFO) basis, with specific procedures for handling overflow and underflow conditions.

Uploaded by

jons86023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Expt4 DS

The document outlines an experiment to implement a Linear Queue Abstract Data Type (ADT) using an array. It includes the theory behind queues, algorithms for insertion and deletion, a source code example in C, and concludes with the principles of queue operations. The queue operates on a First In First Out (FIFO) basis, with specific procedures for handling overflow and underflow conditions.

Uploaded by

jons86023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO: 4

Date of Performance:
Date of Submission :

AIM: Implement Linear Queue ADT Using Array

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:

Algorithm to insert an element in a queue:

Step 1 : IF REAR = MAX-1


Write OVERFLOW
Goto step 4
[END OF IF]
Step 2 : IF FRONT = -1 and REAR = -1
SET FRONT = REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3 : SET QUEUE[REAR] = NUM
Step 4 : EXIT

Algorithm to delete an element from a queue:

Step 1 : IF FRONT = -1 OR FRONT > REAR


Write UNDERFLOW
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2 : EXIT
EXAMPLE:

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:

Write a program to implement Linear Queue ADT using Array

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.

Marks & Signature:

R1 (4 Marks) R2 (4 Marks) R3 (4 Marks) R4 (3 Marks) Total Signature


(15 Marks)

You might also like