Study Abroad Education Loan College News Placement Book Shops E - Books Engg-Admission Edu-Classifieds Students-Forum
Easy Studies
 « NS2  Projects »

## C Program To Implement Circular Queue Using Array:

//Program for Circular Queue implementation through Array #include #include #include #define MAXSIZE 5 int cq[MAXSIZE]; int front,rear; void main() { void add(int,int); void del(int); int will=1,i,num; front = -1; rear = -1; clrscr(); printf("\nProgram for Circular Queue demonstration through array"); while(1) { printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT"); printf("\n\nENTER YOUR CHOICE : "); scanf("%d",&will); switch(will) { case 1: printf("\n\nENTER THE QUEUE ELEMENT : "); scanf("%d",&num); add(num,MAXSIZE); break; case 2: del(MAXSIZE); break; case 3: exit(0); default: printf("\n\nInvalid Choice . "); } } //end of outer while } //end of main void add(int item,int MAX) { //rear++; //rear= (rear%MAX); if(front ==(rear+1)%MAX) { printf("\n\nCIRCULAR QUEUE IS OVERFLOW"); } else { if(front==-1) front=rear=0; else rear=(rear+1)%MAX; cq[rear]=item; printf("\n\nRear = %d Front = %d ",rear,front); } } void del(int MAX) { int a; if(front == -1) { printf("\n\nCIRCULAR QUEUE IS UNDERFLOW"); } else { a=cq[front]; if(front==rear) front=rear=-1; else front = (front+1)%MAX; printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",a); printf("\n\nRear = %d Front = %d ",rear,front); } }

SAMPLE INPUT OUTPUT.

1. INSERTION
2.DELETION
3.EXIT

ENTER THE QUEUE ELEMENT : 10

Rear=0     Front=0

1. INSERTION
2.DELETION
3.EXIT

ENTER THE QUEUE ELEMENT : 20

Rear=1     Front=0

1. INSERTION
2.DELETION
3.EXIT

ENTER THE QUEUE ELEMENT : 30

Rear=2     Front=0

1. INSERTION
2.DELETION
3.EXIT

ENTER THE QUEUE ELEMENT : 40

Rear=3     Front=0

1. INSERTION
2.DELETION
3.EXIT

ENTER THE QUEUE ELEMENT : 50

Rear=4    Front=0

1. INSERTION
2.DELETION
3.EXIT

ENTER THE QUEUE ELEMENT : 60

CIRCULAR QUEUE IS OVERFLOW.

1. INSERTION
2.DELETION
3.EXIT

DELETED ELEMENT FROM QUEUE IS : 10

Rear =4    Front=1

1. INSERTION
2.DELETION
3.EXIT

DELETED ELEMENT FROM QUEUE IS : 20

Rear =4    Front=2

1. INSERTION
2.DELETION
3.EXIT

DELETED ELEMENT FROM QUEUE IS : 30

Rear =4    Front=3

1. INSERTION
2.DELETION
3.EXIT

DELETED ELEMENT FROM QUEUE IS : 40

Rear =4    Front=4

1. INSERTION
2.DELETION
3.EXIT

DELETED ELEMENT FROM QUEUE IS : 50

Rear =-1    Front=-1