0% found this document useful (0 votes)
6 views

Lab Program 6.Docx

E

Uploaded by

prakruthiu646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Program 6.Docx

E

Uploaded by

prakruthiu646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB PROGRAM 6

Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

ABOUT THE EXPERIMENT:

Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last
node is connected back to the first node to make a circle. Circular linked list fallow the First In
First Out principle. Elements are added at the rear end and the elements are deleted at front end
of the queue. The queue is considered as a circular queue when the positions 0 and MAX-1 are
adjacent. Any position before front is also after rear.

PROGRAM CODE :
#include <stdio.h>
#include <stdlib.h>
#define size 5
char queue[size];
void insertq( char);
void deleteq( );
void display( );
int front = - 1;
int rear = - 1;
int main()
{
int choice;
char item,ch;
while(1)
{
printf("\n\n Circular Queue Operations\n");
printf(" 1. Insert \n 2. Delete \n 3. Display\n 4. Exit");
printf(" Enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
ch=getchar();
printf("enter the item to be inserted\n");
scanf("%c",&item);
insertq(item);
break;
case 2:
deleteq( );
break;
case 3:
display( );
break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
}
}

void insertq( char item)


{
if ((front == 0 && rear == size - 1) || (front == rear + 1))
{
printf("queue is full");
return;
}
else if (rear == - 1)
{
rear++;
front++;
}
else if (rear == size - 1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}

void display( )
{
int i;
printf("\n");
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%c\t", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%c\t", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%c\t", queue[i]);
}
}

void deleteq( )
{
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)// only one element in queue
{
printf("%c deleted\n", queue[front]);
front = - 1;
rear = - 1;
}
else
if(front> rear&& front==size-1)
{

printf("%c deleted\n", queue[front]);


front=0;

}
else { printf("%c deleted\n", queue[front]);
front++;

}
}

You might also like