C Program for Circular QUEUE Operations

Data structures using C,
C Program to implement circular queue. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front end. In circular queue is connected to end to end, i,e rear and front end are connected. Compare to normal queue, Circular queue is more advantages. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#define SIZE 5 /* Size of Circular Queue */
int CQ[SIZE], f = -1, r = -1; /* Global declarations */

CQinsert(int elem) { /* Function for Insert operation */
if (CQfull())
printf("nn Overflow!!!!nn");
else {
if (f == -1)
f = 0;
r = (r + 1) % SIZE;
CQ[r] = elem;
}
}

int CQdelete() { /* Function for Delete operation */
int elem;
if (CQempty()) {
printf("nnUnderflow!!!!nn");
return (-1);
} else {
elem = CQ[f];
if (f == r) {
f = -1;
r = -1;
} /* Q has only one element ? */
else
f = (f + 1) % SIZE;
return (elem);
}
}

int CQfull() { /* Function to Check Circular Queue Full */
if ((f == r + 1) || (f == 0 && r == SIZE - 1))
return 1;
return 0;
}

int CQempty() { /* Function to Check Circular Queue Empty */
if (f == -1)
return 1;
return 0;
}

display() { /* Function to display status of Circular Queue */
int i;
if (CQempty())
printf(" n Empty Queuen");
else {
printf("Front[%d]->", f);
for (i = f; i != r; i = (i + 1) % SIZE)
printf("%d ", CQ[i]);
printf("%d ", CQ[i]);
printf("<-[%d]Rear", r);
}
}

main() { /* Main Program */
int opn, elem;
do {
clrscr();
printf("n ### Circular Queue Operations ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the element to be Inserted ?");
scanf("%d", &elem);
CQinsert(elem);
break;
case 2:
elem = CQdelete();
if (elem != -1)
printf("nnDeleted Element is %d n", elem);
break;
case 3:
printf("nnStatus of Circular Queuenn");
display();
break;
case 4:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}

Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

4 comments on “C Program for Circular QUEUE Operations

  • In a linear queue, a new element is inserted at the end of the list, while a deletion is made at the front of the list. The front and rear
    ends are responsible for tracking the queue status. A queue can have a finite number of elements, which is predefined.
    Every new insertion must pass a "queue overflow" test, and likewise, prior to a deletion, a "queue underflow" test must be passed.
    "Queue overflow" checks whether that there is space for the insertion, and "queue underflow" makes sure there are elements waiting to be deleted and the
    queue is not already empty.
    In linear queue if the size is defined, and if you once got the overflow condition, then you can't insert the items even if you delete the items from the queue…..
    see the link and try with different inputs
    http://www.c-program-example.com/2011/10/c-program-for-simplelinear-queue.html

    In a circular queue, insertions and deletions can happen at any position in the queue and not necessarily in a sequential order.
    In circular queue you can add and delete the queue elements up to the size of the queue… i.e in the above program 5.

    You try both the programs i.e linear and circular… you can easily finds which one is the circular!!!

    Reply

Leave a Reply