0% found this document useful (0 votes)
10 views

Circular Queue

Circular Queue using Array

Uploaded by

hodpgcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Circular Queue

Circular Queue using Array

Uploaded by

hodpgcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Figure 3.

4: Circular queue of n elements and four jobs J1, J2, J3, J4


Using the modulo operator which computes remainders, this is just
rear= (rear + 1)mod n. Similarly,
it will be necessary to move front one position clockwise each time a deletion is made. Again,
using the modulo operation, this can be accomplished by
front = (front + l) mod n.
An examination of the algorithms indicates that addition and deletion can now be carried out in
a fixed amount of time or O(1).

procedure ADDQ(item, Q, n, front, rear)


//insert item in the circular queue stored in Q(0:n - 1);
//rear points to the last item and front is one position
// counterclockwise from the first item in Q

rear = (rear + l) mod n //advance rear clockwise//


if front == rear then call QUEUE-FULL
Q(rear) = item //insert new item//
end ADDQ

procedure DELETEQ(item, Q, n, front, rear)


//removes the front element of the queue Q(0:n - 1)//
if front == rear then call QUEUE-EMPTY
front = (front + 1) mod n //advance front clockwise//
item = Q(front) //set item to front of queue//
end DELETEQ
Circular Queue
#include<stdio.h>
#define MAXSIZE 5
int cirQ[MAXSIZE],front=0,rear=0;
void insert(int x)
{
rear = (rear+1) % MAXSIZE;
printf("front = %d Rear = %d\n",front,rear);
if ( front == rear )
printf("Queue is FULL");
else
cirQ[rear] = x;
}
void del()
{
if ( front == rear )
printf("Queue is EMPTY");
else
{
front = (front+1) % MAXSIZE;
printf("Deleted Element = %d\n",cirQ[front]);
}
printf("front = %d Rear = %d\n",front,rear);
}
void main()
{
int choice,x;
do {
clrscr();
printf("\t\t\t\tCircular Queue Operation\n");
printf("\t\t\tMenu\n");
printf("1 >> Insertion.\n");
printf("2 >> Deletion. \n");
printf("3 >> Exit.\n");
printf("\t\tEnter Your Choice :: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter a value :");
scanf("%d",&x);
insert(x);
break;
case 2: del();
break;
case 3: exit(0);
default : printf("Invalid Choice");
}
getch();
}while(1);
}
Circular Queue
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 6
main()
{
int front=-1,rear=-1,x,a[10],i,j,op;
char ch='Y';
clrscr();
do
{
printf("\nCircular Queue Operations\n");
printf("\n1.INSERT\n");
printf("\n2.DELETE\n");
printf("\n3.TRAVERSE\n");
printf("\n4.EXIT\n");

printf("enter your option[1..4]");


scanf("%d",&op);
switch(op)
{
case 1:
if ((front==0 && rear==max-1) || (front == rear+1))
printf("queue overflow");
else
{
printf("enter the element to be inserted");
scanf("%d",&x);
if (front==-1)
front=0;
if (rear==max-1)
rear=0;
else
rear=rear+1;
a[rear]=x;
printf("element successfully inserted");
}
break;
case 2: if (front==-1)
printf("queue underflow");
else
{
printf("The deleted element is %d",a[front]);
if(front==rear)
front=rear=-1;
else
if (front==max)
front=0;
else
front=front+1;
}
break;

case 3: if (front==-1)
printf("queue empty");
else
if (front>=rear)
{
for(i=front;i<=max-1;i++)
printf("\t%d",a[i]);
for(i=0;i<=rear;i++)
printf("\t%d",a[i]);
}
else
for(i=front;i<=rear;i++)
printf("\t%d",a[i]);
break;

case 4: exit(0);
}
printf("\ndo you wish to continue[Y/N]");
scanf("%c",&ch);
}while(ch!='N');
getch();
}

You might also like