Java/J2EE and SOA (348 Blogs) Become a Certified Professional

How To Implement Circular Queue in C?

Last updated on Mar 29,2022 137.8K Views

How To Implement Circular Queue in C?

A circular queue in C stores the data in a very practical manner. It is a linear data structure. It is very similar to the queue. The only difference is that the last node is connected back to the first node. Thus it is called a circular queue. This article will help you explore this concept in detail.

Following pointers will be covered in this article,

So let us get started,

Circular Queue In C

A circular queue solved the limitations of the normal queue. Thus making it a better pick than the normal queue. It also follows the first come first serve algorithm. Circular Queue is also called ring Buffer. 

Operations On A Circular Queue

  1. Enqueue- adding an element in the queue if there is space in the queue.
  2. Dequeue- Removing elements from a queue if there are any elements in the queue
  3. Front- get the first item from the queue.
  4. Rear- get the last item from the queue.
  5. isEmpty/isFull- checks if the queue is empty or full.

Applications Of A Circular Queue

  • Memory management: circular queue is used in memory management.
  • Process Scheduling: A CPU uses a queue to schedule processes.
  • Traffic Systems: Queues are also used in traffic systems.

Sample Code

#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("Queue Underflown");
return ;
}
printf("Element deleted from queue is : %dn",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is emptyn");
return;
}
printf("Queue elements :n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos])
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("n");
}
int main()
{
int choice,item;
do
{
printf("1.Insertn");
printf("2.Deleten");
printf("3.Displayn");
printf("4.Quitn");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choicen");
}
}while(choice!=4);
return 0;
}

Output:

Circular queue code

Explanation

This code is a menu-driven implementation of a circular queue. First, define the size of MAX variable to be 5. Then, the array called cqueue_array is declared of size MAX. Three functions are to be declared. The functions are insert, display and delete functions. There is a menu-driven main function. The user is asked to enter his choice and call the appropriate function to perform the task.

There are 2 pointers, the front is at the front of the queue and rear is at the back of the queue. In a circular queue, the elements are added from the back of the queue and remove them from the front of the queue. This is the circular queue so if the rear is at the last location, if incremented it will be pointing to the first element

Moving on with this article on Circular Queue In C

Insert Function

void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}

In the insertion part, check if the circular queue is full, if yes give the overflow message or else  check if the circular queue is empty. If rear is at the last location it is made to point to the first or else Rear is incremented by one. This is how it becomes a circular queue. At the end the item is entered into the queue.

Moving on with this article on Circular Queue In C

Delete Function

void deletion()
{
if(front == -1)
{
printf("Queue Underflown");
return;
}
printf("Element deleted from queue is : %dn",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}

 

In the delete part, it is first checked if the circular queue is empty. If yes,  print underflow error, that is queue is empty. Otherwise print the first element, that is the element that will be deleted and increment front. This is how deletion takes place.

Then, check if front and rear are pointing at the same location and assign both values to -1, that is not pointing anywhere. Otherwise, check if the front is at the end, if yes, make it point to the first location. Else, increment front.

Moving on with this article on Circular Queue In C

Display Function

void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is emptyn");
return;
}
printf("Queue elements :n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("n");
}

We just display the circular queue like how an array is displayed. Check if the circular queue is empty here as well. Then, print the elements of the circular queue according to the position of the variables front and rear.

This brings us to the end of this article on Circular Queue In C.

With this we come to the end of this article on ‘Queue In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.Implement the above code with different strings and modifications. Now, we have a good understanding of all key concepts related to the pointer.

Got a question for us? Mention them in the comments section of  this article and we will get back to you.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Circular Queue in C?

edureka.co