Circular Queue Representation using Array

A circular queue is a linear data structure that follows FIFO principle. In circular queue, the last node is connected back to the first node to make a circle. In Circular queue elements are added at the rear and elements are deleted from front.

circular queue program in c

We can represent circular queue using array as well as linked list. In this article we will see how we can implement circular queue using array and we will see writing program for it.

Below C Program has three methods to perform circular queue operations

enqueue(int val):- This function will insert a value at the rear of the circular queue. Before insertion, it will checks whether the circular queue is empty or not. If a queue is empty then print “Queue Overflow” otherwise new item will be added to the circular queue.

dequeue():- This function will delete items from the front. Before deletion, it will checks whether the circular queue is empty or not. If it is empty then “Queue Underflow” will be print otherwise dequeue operation will be performed.

display():- display() function will first check circular queue is empty or not? If it is empty then print “Queue is Empty” and if not empty then print all the items of the circular queue.

C Program to implement Circular Queue using Array

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int cqueue[6];
int front = -1, rear = -1, n=6;
void enqueue(int val){
    if ((front == 0 && rear == n-1) || (front == rear+1)) {
      printf("Queue Overflow \n");
      return;
   }
   if (front == -1) {
      front = 0;
      rear = 0;
   }
   else {
      if (rear == n - 1)
         rear = 0;
      else
         rear = rear + 1;
   }
   cqueue[rear] = val ;
}
void dequeue(){
    if (front == -1) {
       printf("Queue Underflow\n");
      return ;
   }
    printf("Element deleted from queue is : %d ", cqueue[front]);
   if (front == rear) {
      front = -1;
      rear = -1;
   }
   else {
      if (front == n - 1)
         front = 0;
      else
         front = front + 1;
   }
}

void display(){
   int f = front, r = rear;
   if (front == -1) {
       printf("Queue is empty");
      return;
   }
   printf("Queue elements are :\n");
   if (f <= r) {
      while (f <= r){
          printf("%d", cqueue[f]);
         f++;
      }
   }
   else {
      while (f <= n - 1) {
          printf("%d",cqueue[f]);
         f++;
      }
      f = 0;
      while (f <= r) {
          printf("%d",cqueue[f]);
         f++;
      }
   }
  
}  
  
int menu(){
    int choice;
    printf("\n 1.Add value to the list");
    printf("\n 2. Delete value to the list");
    printf("\n 3. Travesre/View List");
    printf("\n 4. exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
void main(){
    int value;
    while(1){
        switch(menu()){
            case 1:
           printf("Input for insertion: ");
            scanf("%d",&value);
                enqueue(value);
                break;
            case 2:
                dequeue();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(0);
            default:
                printf("invalid choice");
        }
    }
    getch();
}

Share on