/* Before understanding the concept of circular queue,you must visit my previous
program on "OPERATIONS ON QUEUE",which discusses how operations are carried out on linear queue.
This program shows how even after the queue is FULL, insertions can be made after deletion.
This is the advantage of circular queue over linear queue!! */
#include<stdio.h>
#include<conio.h>
#define MAX 2 //defining size of queue
void insert(int);
int del();
void display();
int queue[MAX];
int front=-1;
int rear=-1;
main()
{
int choice,num;
while(1) //keep continuing the program until user wants to exit
{
printf("Enter your choice \n");
printf("1.INSERTION \n");
printf("2.DELETION \n");
printf("3.DISPLAY \n");
printf("4. EXIT \n");
scanf("%d",&choice);
switch(choice)
{
case 1: //executes insertion
insert(num);
break;
case 2: //executes deletion
num=del();
break;
case 3: //executes display
display();
break;
case 4: //stop further processing
exit(1);
break;
default:
printf("Invalid choice");
}
}
}
// Function definition //
void insert(int x)
{
if((front==0 && rear==MAX-1) || (front==rear+1))/* if either no deletion has occured and we have
reached the end of the queue OR after all deletions,we have inserted new elements that have reached
the queue's capacity */
{
printf("\n Queue is FULL \n ");
return;
}
else
{
printf("Enter number to be inserted: ");
scanf("%d",&x);
}
if (front==-1) //for inserting 1st element we set front=0
{
front=0;
}
if(rear==MAX-1) //if we have reached the end of the queue
{
rear=0; //we reset rear=0,as it is a circular queue
}
else
{
rear=rear+1;
}
queue[rear]=x; //insertion at end of queue
}
int del()
{
int x;
if(front==-1)
{
printf("\n Queue is EMPTY \n ");
}
x=queue[front]; /* since queue is FIFO and deletion occurs from front end,so we set
element at front of the queue as a value to the variable x */
if(front==rear) //if there is only 1 element in the queue
{
front=-1;//deleting that element
rear=-1;
}
else if(front==MAX-1) /* one last element at the "end of the queue" is left
which is soon going to be deleted by making front +1,we further reset the value
of front=0 after the last deletion as it is a circular queue and now front is
once again ready to delete newly entered elements in the queue */
{
front=0;
}
else
{
front=front+1;
}
printf("%d has been deleted \n",x);
return x;
}
void display()
{
if(front==-1)
{
printf("Queue is Empty");
return;
}
int i;
i=front;
if(front<=rear)
{
while(i<=rear) //setting a condition for printing entire queue
{
printf("%d\n",queue[i++]);
}
}
else //touches the concept of circular queue
{
while(i<=MAX-1) //if we are somewhere in between the queue
{
printf("%d\n",queue[i++]);
} //this will go on till last element
i=0; //we reset front =0
while(i<=rear)
{
printf("%d\n",queue[i++]);
}
}
}
program on "OPERATIONS ON QUEUE",which discusses how operations are carried out on linear queue.
This program shows how even after the queue is FULL, insertions can be made after deletion.
This is the advantage of circular queue over linear queue!! */
#include<stdio.h>
#include<conio.h>
#define MAX 2 //defining size of queue
void insert(int);
int del();
void display();
int queue[MAX];
int front=-1;
int rear=-1;
main()
{
int choice,num;
while(1) //keep continuing the program until user wants to exit
{
printf("Enter your choice \n");
printf("1.INSERTION \n");
printf("2.DELETION \n");
printf("3.DISPLAY \n");
printf("4. EXIT \n");
scanf("%d",&choice);
switch(choice)
{
case 1: //executes insertion
insert(num);
break;
case 2: //executes deletion
num=del();
break;
case 3: //executes display
display();
break;
case 4: //stop further processing
exit(1);
break;
default:
printf("Invalid choice");
}
}
}
// Function definition //
void insert(int x)
{
if((front==0 && rear==MAX-1) || (front==rear+1))/* if either no deletion has occured and we have
reached the end of the queue OR after all deletions,we have inserted new elements that have reached
the queue's capacity */
{
printf("\n Queue is FULL \n ");
return;
}
else
{
printf("Enter number to be inserted: ");
scanf("%d",&x);
}
if (front==-1) //for inserting 1st element we set front=0
{
front=0;
}
if(rear==MAX-1) //if we have reached the end of the queue
{
rear=0; //we reset rear=0,as it is a circular queue
}
else
{
rear=rear+1;
}
queue[rear]=x; //insertion at end of queue
}
int del()
{
int x;
if(front==-1)
{
printf("\n Queue is EMPTY \n ");
}
x=queue[front]; /* since queue is FIFO and deletion occurs from front end,so we set
element at front of the queue as a value to the variable x */
if(front==rear) //if there is only 1 element in the queue
{
front=-1;//deleting that element
rear=-1;
}
else if(front==MAX-1) /* one last element at the "end of the queue" is left
which is soon going to be deleted by making front +1,we further reset the value
of front=0 after the last deletion as it is a circular queue and now front is
once again ready to delete newly entered elements in the queue */
{
front=0;
}
else
{
front=front+1;
}
printf("%d has been deleted \n",x);
return x;
}
void display()
{
if(front==-1)
{
printf("Queue is Empty");
return;
}
int i;
i=front;
if(front<=rear)
{
while(i<=rear) //setting a condition for printing entire queue
{
printf("%d\n",queue[i++]);
}
}
else //touches the concept of circular queue
{
while(i<=MAX-1) //if we are somewhere in between the queue
{
printf("%d\n",queue[i++]);
} //this will go on till last element
i=0; //we reset front =0
while(i<=rear)
{
printf("%d\n",queue[i++]);
}
}
}