F How we can implement a Circular Queue using an integer array ? | CodeTheta

How we can implement a Circular Queue using an integer array ?

April 20, 2014

This program is implement by Circular Queue and it will be implement by integer array, in this program three operation will done when you will compile this program..
1. Insert the element
2. Delete the element
3. Display the element

This program uses several functions. isempty() use for underflow method, isfull() use for overflow method, del() use for delete any element for circular queue. this program uses switch case method.

Program coding part divided in to three parts. First parts related to "Insert the element" to a fixed array. Second part related to "Delete the element" from the array and the third option or method is "Display the element" which is reside in the array.

The output of this program is below


/* http://native-code.blogspot.com */
#include<stdio.h>
#include<conio.h>
#define MAX 20
int f,r,q[MAX];
int isempty();
int isfull();
int del();
void insert(int);
void display();
void main()
{
int k,ch,d;
k=ch=d=0;
f=-1;r=-1;
while(1)
{
printf("/* http://native-code.blogspot.com */");
printf("\n1: Insert\n2: Delete\n3: Display\n4: EXIT\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(isfull()==0)
{
printf("Enter element:\n");
scanf("%d", &k);
insert(k);
}
else
printf("Overflow\n");
break;
case 2:
if(isempty()==0)
{
d=del();
printf("Deleted no. is: %d\n",d);
}
else
printf("Underflow\n");
break;
case 3:
display();
printf("\n");
break;
case 4:
exit(0);
default:
printf("Wrong choice.\n");
}
}
}
void insert(int k)
{
if(isempty())
r=f=0;
else
{
if(r==(MAX-1) && f>0)
{
r=0;
}
else
r++;
}
q[r]=k;
}
int del()
{
int temp;
temp=q[f];
if(r==f)
f=r=-1;
else if(f==MAX-1 && r<MAX-1)
f=0;
else
f++;
return(temp);
}
int isfull()
{
if((r==(MAX-1) && f==0) ||  r==(f-1))
return(1);
else
return(0);
}
int isempty()
{
if(f==-1 && r==-1)
return(1);
else
return(0);
}
void display()
{
int i;
if(r<f)
{
for(i=f; i<=MAX-1; i++)
printf("%d ", q[i]);
for(i=0;i<=r;i++)
printf("%d ",q[i]);
}
else
{
for(i=f; i<=r; i++)
printf("%d ", q[i]);
}
}
/* http://native-code.blogspot.com */

Post a Comment