C program to implement the Circular Queue

Circular Queue with its Operation

Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle.

Graphical representation of a circular queue is as follows...

Implementation of Circular Queue

To implement a circular queue data structure using array, we first perform the following steps before we implement actual operations.
  • Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with specific value.
  • Step 2: Declare all user defined functions used in circular queue implementation.
  • Step 3: Create a one dimensional array with above defined SIZE (int cQueue[SIZE])
  • Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear = -1)
  • Step 5: Implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on circular queue.

enQueue() : Inserting value in queue

This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position.
    Steps:
  1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
  2. If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.

deQueue() : Deleting Value from queue

This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position.
    Steps:
  1. Check whether queue is Empty means check (front==-1).
  2. If it is empty then display Queue is empty. If queue is not empty then step 3
  3. Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is true then set front=0 and return the element.

dispaly() : Display the values present in queue

We can use the following steps to display the elements of a circular queue...
  • Step 1: Check whether queue is EMPTY. (front == -1)
  • Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
  • Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
  • Step 4: Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.
  • Step 5: If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.
  • Step 6: Set i to 0.
  • Step 7: Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.
Let us check out the code :)


#include <stdio.h> 
#include <conio.h>
int i,f=-1,r=-1,x,c,cq[100],n;
void insert();
void del();
void display();
void main()
{
	clrscr();
	printf("\nEnter the size of Circular Queue: ");
	scanf("%d",&n);
	while(1)
	{
		printf("\n1).Insert   2).Delete   3).Display   4).Exit\n");
		printf("Enter your choice: ");
		scanf("%d",&c);
		switch(c)
		{
			case 1:
				insert();
				break;
			case 2:
				del();

				break;
			case 3:
				display();
				break;
			case 4:
				exit(0);
			default:
				printf("invalid entry");
		}
	}
}
void insert()
{
	if((f==0 && r==n-1) || f==r+1)
	{
		printf("Circular queue is overflow");
	}
	else
	{
	if(f==-1)
	{
		f=0;
		r=0;
	}
	else
	{
		if(r==n-1)
			r=0;
		else
			r=r+1;
	}
	printf("Enter the value:");
	scanf("%d",&x);
	cq[r]=x;
	}
}
void del()
{
	if(f==-1)
	{
		printf("Circular Queue is underflow");
	}
	else
	{
		printf("Deleted element is: %d",cq[f]);
		cq[f]=NULL;
		if(f==r)
		{
			f=r=-1;
		}
		else
		{
			if(f==n-1)
				f=0;
			else
				f++;
		}
	}
}
void display()
{
	if(f==-1)
	{
		printf("Circular Queue is empty");
	}
	else
	{
		printf("Inputed elements are: \n");
		for(i=0;i<=r;i++)
		{
			printf("%d\t",cq[i]);
		}
	}
}



















Comments

Popular posts from this blog

MultiSelection of Item in Recycler View

Upload Image From Android To Php Server

Merge Sort