Friday, 23 January 2015

circular queue ADT using an array.


import java.util.*;
class CirQue
{
int front,rear,next=0;
int que[];
int max,count=0;
CirQue(int n)
{
max=n;
que=new int[max];
front=rear=-1;
}
boolean isfull()
{
if(front==(rear+1)%max)
return true;
else
return false;
}
boolean isempty()
{
if(front==-1&&rear==-1)
return true;
else
return false;
}
int delete()
{
if(isempty())
{
return -1;
}
else
{
count --;
int x=que[front];
if(front==rear)
front=rear=-1;
else
{
next=(front+1)%max;
front=next;
}
return x;
}
}
void insert(int item)
{
if(isempty())
{
que[++rear]=item;
front=rear;
count ++;
}
else if(!isfull())
{
next=(rear+1)%max;
if(next!=front)
{
que[next]=item;
rear=next;
}
count ++;
}
else
System.out.println("q is full");
}
void display()
{
if(isempty())
System.out.println("queue is empty");
else
next=(front)%max;
while(next<=rear)
{
System.out.println(que[next]);
next++;
}
}
int size()
{
return count;
}
public static void main(String args[])
{
int ch;
Scanner s=new Scanner(System.in);
System.out.println("enter limit");
int n=s.nextInt();
CirQue q=new CirQue(n);
do
{
System.out.println("1.insert");
System.out.println("2.delete");
System.out.println("3.display");
System.out.println("4.size");
System.out.println("enter ur choice :");
ch=s.nextInt();
switch(ch)
{
case 1:System.out.println("enter element :");
int n1=s.nextInt();
q.insert(n1);
break;
case 2:int c1=q.delete();
if(c1>0)
System.out.println("deleted element is :"+c1);
else
System.out.println("can't delete");
break;
case 3:q.display();
break;
case 4:System.out.println("queue size is "+q.size());
break;
}
}
while(ch!=0);
}
}



No comments:

Post a Comment