Circular Queue using Array in C

Circular Queue in C

Implementation of Circular Queues using Array in C

In this post we will learn on how we can implement circular queue using array in C . Circular queues are extension of linear queues where the max size of queue is always available for insertion. Unlike linear queues which faces the problem of reduction in available size for insertion with each iterative dequeue operation that happens, we will learn more about the same in this post.

Understanding why Linear Queues are not suitable ?

Image representation of the above.

Why Linear Queues are not suitable

Circular Queues Implementation using Arrays in C

We can make our code circular by making minor adjustment and changes in the code.

For example, our code can try to enter rear item the following ways –

rear = (rear + 1)%size

In this case if the queue is full, it will go to 0th position, if 0th position is empty.

Circular Queue using Array in C

Code for Circular Queues implementation using Arrays

Run
#include<stdio.h>

#define capacity 6

int queue[capacity];
int front = -1, rear = -1;

// Here we check if the Circular queue is full or not
int checkFull ()
{
  if ((front == rear + 1) || (front == 0 && rear == capacity - 1))
    {
      return 1;
    }
  return 0;
}

// Here we check if the Circular queue is empty or not
int checkEmpty ()
{
  if (front == -1)
    {
      return 1;
    }
  return 0;
}

// Addtion in the Circular Queue
void enqueue (int value)
{
  if (checkFull ())
    printf ("Overflow condition\n");

  else
    {
      if (front == -1)
	front = 0;

      rear = (rear + 1) % capacity;
      queue[rear] = value;
      printf ("%d was enqueued to circular queue\n", value);
    }
}

// Removal from the Circular Queue
int dequeue ()
{
  int variable;
  if (checkEmpty ())
    {
      printf ("Underflow condition\n");
      return -1;
    }
  else
    {
      variable = queue[front];
      if (front == rear)
	{
	  front = rear = -1;
	}
      else
	{
	  front = (front + 1) % capacity;
	}
      printf ("%d was dequeued from circular queue\n", variable);
      return 1;
    }
}

// Display the queue
void print ()
{
  int i;
  if (checkEmpty ())
    printf ("Nothing to dequeue\n");
  else
    {
      printf ("\nThe queue looks like: \n");
      for (i = front; i != rear; i = (i + 1) % capacity)
	{
	  printf ("%d ", queue[i]);
	}
      printf ("%d \n\n", queue[i]);

    }
}

int main ()
{
  // Not possible as the Circular queue is empty
  dequeue ();

  enqueue (15);
  enqueue (20);
  enqueue (25);
  enqueue (30);
  enqueue (35);

  print ();
  dequeue ();
  dequeue ();

  print ();

  enqueue (40);
  enqueue (45);
  enqueue (50);
  enqueue (55);			//Overflow condition
  print ();

  return 0;
}

Output

Underflow condition
15 was enqueued to circular queue
20 was enqueued to circular queue
25 was enqueued to circular queue
30 was enqueued to circular queue
35 was enqueued to circular queue

The queue looks like: 
15 20 25 30 35 

15 was dequeued from circular queue
20 was dequeued from circular queue

The queue looks like: 
25 30 35 

40 was enqueued to circular queue
45 was enqueued to circular queue
50 was enqueued to circular queue
Overflow condition

The queue looks like: 
25 30 35 40 45 50 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java