C Program for Circular QUEUE Operations – using Array

Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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("\n\n Overflow!!!!\n\n");
else
{
if(f==-1)f=0;
r=(r+1) % SIZE;
CQ[r]=elem;
}
}
int CQdelete()
{                      /* Function for Delete operation */
int elem;
if(CQempty()){ printf("\n\nUnderflow!!!!\n\n");
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 Queue\n");
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 ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
CQinsert(elem); break;
case 2: elem=CQdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Circular Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n  Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
Ansten Lobo

5 thoughts on “C Program for Circular QUEUE Operations – using Array

  1. The only correct working example from many so called ‘programming’ websites.. ThanQ

  2. i got a problem with the display function:
    1. when there is only one element in the Queue, as in(f==r)..the loop wont run since(i=f; i!=r….)
    2. this loop wont print the last element anyway, because the loop is meant to stop at i=r..hence not printing the last element (full or not)!
    Pls help(:facing the same problem here:)
    Thank You!

  3. solved::
    printf(“\nYour Que is: front->”);
    for(i=front; ; i=(i+1)%max){
    printf(” %d”, que[i]);
    if(i==rear)break;
    }
    printf(” <-rear\n");

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in