0% found this document useful (0 votes)
5 views6 pages

Circular Queue Bluej Program

The document describes the implementation of a circular queue in Java, detailing the class CirQueue with methods for adding (push) and removing (pop) integers. The push method adds an integer to the rear while checking for full capacity, and the pop method removes an integer from the front, returning -9999 if the queue is empty. Additionally, a show method displays the current elements in the queue.

Uploaded by

aadwikkashyap320
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Circular Queue Bluej Program

The document describes the implementation of a circular queue in Java, detailing the class CirQueue with methods for adding (push) and removing (pop) integers. The push method adds an integer to the rear while checking for full capacity, and the pop method removes an integer from the front, returning -9999 if the queue is empty. Additionally, a show method displays the current elements in the queue.

Uploaded by

aadwikkashyap320
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Circular Queue

A circular queue is a linear data structure which


works on the principle of FIFO, enables the
user to enter data from the rear end and
remove data from the front end with the rear
end connected to the front end to form a
circular pattern.
Define a class CirQueue with the following
details:
Class name: CirQueue
Data members/instance variables:
cq[]: array to store the integers
cap: stores the maximum capacity of the array
front: to point the index of the front end
rear: to point the index of the rear end
Member functions:
CirQueue(int max): constructor to initialize the
data member cap = max, front = 0 and rear =
0
void push(int n): to add integer in the queue
from the rear end if possible, otherwise
display the message “QUEUE IS FULL”
int pop(): removes and returns the integer
from the front end of the queue if any, else
returns -9999
void show(): displays the queue elements
Specify the class CirQueue giving details of the functions
void push(int) and int pop(). Assume that the other
functions have beendefined.
The main() function and algorithm need not be written.

import [Link].*;
class CirQ
{
int cq[];
int cap;
int front,rear;
public CirQ(int max)
{
cap=max;
cq= new int [cap];
front = rear=0;
}
public void push(int v)
{
if (cap==0)
[Link] ("Queue is full");
else
{
cq[rear]=v;
rear= (rear+1)%[Link];
cap--;
}
}
public int pop()
{
if (cap==[Link])
return -9999;
else
{
int v= cq[front];
front= (front +1)%[Link];
cap++;
if (front ==rear)
front =rear=0;
return v;
}
}
public void show()
{
if(cap == [Link])
[Link] ("Queue is empty");
else
{
int i= front;
int count = [Link];
while (count >cap)
{
[Link] (cq[i]+" ");
i = (i+1)%[Link];
count --;
}
[Link] ();
}
}
public static void main ()
{
Scanner as = new Scanner ([Link]);
[Link] ("Enter Circular queue size");
int size = [Link]();
CirQ obj= new CirQ(size);
[Link] (" Inserting elements in circular queue");
[Link](68);
[Link](46);
[Link](45);
[Link](5);
[Link](15);
[Link] ("Elements in circular queue = ");
[Link]();
int d = [Link]();
[Link] (d+" is deleted from circular queue");
[Link] ("After deletion elements in circular queue = ");
[Link]();
[Link] (" Inserting elements in circular queue after
deletion");
[Link](100);
[Link] ("Finally circular queue looks like= ");
[Link]();
}
}

You might also like