DSA:circular queue
class circularqueue
{
int rear=-1,front=-1,len;
int a[]=new int[50],val,item;
circularqueue(int w)
{
len=w;
}
public void insert(int item)
{
if(front==thenext(rear))
{
System.out.println("Q is fulll");
}
else if(front==-1)
{ front=0;
rear=0;
a[rear]=item;
}
else
{
rear=thenext(rear+1);
a[rear]=item;
System.out.println("inserted element is"+ a[rear]);
}
}
public void delete()
{
if(front==rear)
{
front=-1;
rear=-1;
System.out.println("queue is empty");
}
else
{ val=a[front];
front=thenext(front);
System.out.print("deleted element is:"+val);
}
}
public int thenext(int val)
{
int next=(val+1)%len;
return next;
}
public void display()
{
if(front==-1&&rear==-1)
System.out.println("queue is empty");
else
{
System.out.println("QUEUE contains....");
int i=front;
do
{
System.out.println(a[i]+"\t");
i=thenext(i);
}while(i!=thenext(rear)) ;
}
}
}
No comments:
Post a Comment