Circular queue in c
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.
In a normal Queue, we can insert elements until the queue becomes full. But once the queue becomes full, we can not insert the next element even if there is a space in front of the queue.
Circular Queue in c using array
#include<stdio.h> #define MAX 5 int rear=-1; int front=-1; int cqueue[MAX]; void insert() { int data; if((front==0&&rear==MAX-1)||(front==rear+1)) { printf("\nQueue is Overflow(Full)"); return; } if(front==-1) { front=0; rear=0; } else if(rear==MAX-1) rear=0; else rear=rear+1; printf("\nEnter data:"); scanf("%d",&data); cqueue[rear]=data; } void del() { if(front==-1) { printf("\nQueue is Underflow(Empty)"); } printf("\nDeleted data:%d",cqueue[front]); if(front==rear) { front=-1; rear=-1; } else if(front==MAX-1) front=0; else front=front+1; } void display() { int fposition=front,rposition=rear; if(front==-1) { printf("\nCQueue is empty"); return; } printf("\nCQUEUE Elements:"); if(fposition<=rposition) { while(fposition<=rposition) { printf(" %d",cqueue[fposition]); fposition++; } } else { while(fposition<=MAX-1) { printf(" %d",cqueue[fposition]); fposition++; } fposition=0; while(fposition<=rposition) { printf(" %d" ,cqueue[fposition]); fposition++; } } } int main() { int option; while(1) { printf("\n1.for insert : "); printf("\n2.for delete : "); printf("\n3.for display: "); printf("\n4.for exit...: "); scanf("%d",&option); switch(option) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: return 0; default: printf("\ninvalud option: "); } } }