DS-Record-mca-03) write a program to implement operations on circular queues.

 Program:-03

AIM:-
write a program to implement operations on circular queues. 
code:-
#include <stdio.h>
#include <conio.h>
#define SIZE 3
int items[SIZE];
int front = -1, rear = -1;
// Check if the queue is full
int isFull()
{
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1))
return 1;
return 0;
}
// Check if the queue is empty
int isEmpty()
{
if (front == -1)
return 1;
return 0;
}
// Adding an element
void enQueue(int element)
{
if (isFull())
{
printf("\n Queue is full!! \n");
}
else
{
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("Inserted -> %d\t", element);
}
}
// Removing an element
int deQueue()
{
int element;
if (isEmpty())
{
printf("\n Queue is empty !! \n");
return (-1);
} else
{
element = items[front];
if (front == rear)
{
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else
{
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
// Display the queue
void display()
{
int i;
if (isEmpty())
{
printf(" \n Empty Queue\n");
}else
{
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE)
{
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
//Main function
void main()
{
clrscr();
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// Fails to enqueue because front == rear + 1
enQueue(8);
getch();
}
Output:-

Comments

Popular posts from this blog

digital marketing ppt-u1

SOFTWARE

cn lab

Computer Operations and Performing - D L Unit-1-1

DS-Record-mca-04) write a program for evaluating a given postfix expression using stack.

DBMS Degree Lab Records

Unit 2: Foundations of Ownership, Security Related Concepts in Blockchain

Unit-1 Foundations of Software Systems and Blockchain

Access the Internet to Browse Infromation & E-Mail Operation- D L Unit-2-1

6)what are the various service of internet and protocols ICT-unit-1