0% found this document useful (0 votes)
10 views8 pages

EXPERIMENT NO_-05_compressed

The document describes the implementation and operations of a circular queue, which follows the FIFO principle with the last position connected to the first. It outlines the functions for inserting (enQueue), deleting (deQueue), and displaying elements in the queue, along with the corresponding conditions for overflow and underflow. Additionally, it includes a C program that demonstrates these operations with user interaction.

Uploaded by

roshanishirage
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)
10 views8 pages

EXPERIMENT NO_-05_compressed

The document describes the implementation and operations of a circular queue, which follows the FIFO principle with the last position connected to the first. It outlines the functions for inserting (enQueue), deleting (deQueue), and displaying elements in the queue, along with the corresponding conditions for overflow and underflow. Additionally, it includes a C program that demonstrates these operations with user interaction.

Uploaded by

roshanishirage
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/ 8

EXPERIMENT NO:-05

Date of Performance:

Date of Submission:

A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out)
principle except that the last position is connected to the first position in a circular queue that
forms a circle. It is also known as a Ring Buffer.

Operations on Circular Queue


The following are the operations that can be performed on a circular queue:

○​ Front: It is used to get the front element from the Queue.


○​ Rear: It is used to get the rear element from the Queue.
○​ enQueue(value): This function is used to insert the new value in the Queue. The new
element is always inserted from the rear end.
○​ deQueue(): This function deletes an element from the Queue. The deletion in a
Queue always takes place from the front end.

Step1: IF (REAR+1)%MAX = FRONT


Write " OVERFLOW "
Goto step 4
[End OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]

Step 3: SET QUEUE[REAR] = VAL

Step 4: EXIT

Step 1: IF FRONT = -1
Write "
UNDERFLOW "
Goto Step 4
[END of IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;​ // rear is incremented
queue[rear]=element;​ // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter the element which is to be inserted");


scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue()
; break;
case 3:
display();

}}
return 0;
}

Press 1: Insert an element


Press 2: Delete an element
Press 3: Display the element
Enter your choice 1
Enter the element which is to be inserted 20

Press 1: Insert an element


Press 2: Delete an element
Press 3: Display the element
Enter your choice 1
Enter the element which is to be inserted 30

Press 1: Insert an element


Press 2: Delete an element
Press 3: Display the element
Enter your choice 3

Elements in a Queue are :,20,30,


Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice

Conclusion:
successfully implemented binary search tree with its methods

Sign and Remark:

R1 R2 R3 R4 Total Marks Signature


(3) (5) (4) (3) (15)

You might also like