Skip to content

Instantly share code, notes, and snippets.

@78526Nasir
Created January 28, 2016 04:22
Show Gist options
  • Save 78526Nasir/dbb4d98ad259b09bc8ac to your computer and use it in GitHub Desktop.
Save 78526Nasir/dbb4d98ad259b09bc8ac to your computer and use it in GitHub Desktop.
Circular Queue Implementation in C using array !
//Circular Queue Implementation in C using Array !
#include<stdio.h>
#include<stdlib.h> //header file for using return and exit function
#define max 5 //symbolic constant
int front=-1,rear=-1; // global variable
int CQueue[max];
void insert();
int delete();
void display();
main()
{
int w,no;
for(;;)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. EXIT");
printf("\nEnter what you want :");
scanf("%d",&w);
switch(w)
{
case 1:
insert();
break;
case 2:
no=delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nInvalid Choice !!\n");
}
}
}
void insert()
{
int no;
if((front ==0 && rear == max-1) || front == rear+1)
{
printf("\nCircular Queue Is Full !\n");
return;
}
printf("\nEnter a number to Insert :");
scanf("%d",&no);
if(front==-1)
front=front+1;
if(rear==max-1)
rear=0;
else rear=rear+1;
CQueue[rear]=no;
}
int delete()
{
int e;
if(front==-1)
{
printf("\nThe Circular Queue is Empty !!\n");
return;
}
e=CQueue[front];
if(front==max-1)
front=0;
else if(front==rear)
{
front=-1;
rear=-1;
}
else front=front+1;
printf("\n%d was deleted !\n",e);
return e;
}
void display()
{
int i;
if(front==-1)
{
printf("\nThe Circular Queue is Empty ! Nothing To Display !!\n");
return;
}
i=front;
if(front<=rear)
{
printf("\n\n");
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
else
{
printf("\n\n");
while(i<=max-1)
printf("%d ",CQueue[i++]) ;
i=0;
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment