Circular Queues (Using Arrays)
Implemented using C Language
C Language Code:
C_QUE_AR.H
// DSTC ------ C_QUE_AR.H ( Circular Queues using Arrays )
// it is a header file
#include "assert.h"
typedef struct circular_queues_array
{
int *bfr ;
int size ;
int f,r ;
}cqueue;
cqueue *makecqueue(int n)
{
cqueue *cq ;
cq = (cqueue *)malloc (sizeof(cqueue)) ;
cq->size = n;
cq->f = cq->r = -1 ;
cq->bfr = (int *)malloc(sizeof(int)*n);
return (cq);
}
// INSERTING A VALUE INTO CIRCULAR QUEUE USING ARRAYS....
void insertcqueue(cqueue *cq, int val)
{
int pos ;
pos = (cq->r +1)%cq->size ;
printf("\n Size = %d, pos = %d ",cq->size,pos);
if (pos == cq->f)
{
printf("\n Queue id Full");
}
else
{
if(cq->r == -1)
{
printf("\npos> %d",pos);
cq->f = pos ;
cq->r = pos ;
cq->bfr[cq->r] = val ;
}
else
{
cq->r = pos ;
cq->bfr[cq->r] = val ;
}
}
}
int delcqueue(cqueue *cq)
{
int v;
if (cq->f == -1)
{
printf("\n Queue is Empty");
return 0 ;
}
v = cq->bfr[cq->f];
if(cq->f==cq->r)
cq->f=cq->r=-1;
else
cq->f =(cq->f+1)%cq->size;
return (v);
}
void printcqueue(cqueue *cq)
{
int i;
if(cq->f==-1)
printf("\n The queue is empty") ;
else
{
i=cq->f-1 ;
do
{
i=(i+1)%cq->size ;
printf("\n valueof %d = %d", i, cq->bfr[i]) ;
}
while(i!= cq->r);
}
}
C_QUE_AR.CPP
// DSTC - C_QUE_AR.CPP -- Circular Queues using Arrays...
// including C_QUE_AR.H file
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "c_que_ar.h"
main()
{
cqueue *cq ;
int n,v,i,c ;
clrscr() ;
printf("\n enter the no. of elements in the CirQue Array:") ;
scanf("%d",&v) ;
cq = makecqueue(v) ;
do
{
clrscr();
printf("\n Operations on Circular Queues using Arrays ");
printf("\n ===========================================");
printf("\n\n 1. Add in Circular Queues ");
printf("\n 2. Delete in Circular Queues ");
printf("\n 3. Display all items in Circular Queue ");
printf("\n 0. Exit \n\n\n ") ;
printf("\n Enter your choice: ") ;
scanf("%d",&c);
switch(c)
{
case 0: exit(1);break ;
case 1:
{
printf("\n Enter Value :");
scanf("%d",&n);
insertcqueue(cq,n);
printf("\n Values of Queues after Insertion");
printf("\n==================================\n");
printcqueue(cq);
getchar();getchar();
break ;
}
case 2:
{
delcqueue(cq);
printf("\n Values of Queues after Deletion");
printf("\n==================================\n");
getchar(); getchar();
}
case 3:
{
printf("\n Values of Queues");
printf("\n==================================\n");
printcqueue(cq);
getchar() ; getchar() ;
}
}
}while(c!=0) ;
}
No comments:
Post a Comment