主函数测试
public class criclqueue {
int front;
int rear;
int maxsize;
int[] arr;
public criclqueue(int maxsize)
{
this.maxsize = maxsize;
front = 0;
rear = 0;
arr = new int[maxsize];
}
public void creatqueue(int length)
{
maxsize=length;
arr=new int[maxsize];
}
public boolean isfull()
{
return (rear+1)%maxsize==front;
}
public boolean isempty()
{
return front==rear;
}
public void adddata(int n)
{
if(isfull())
{
System.out.println("队列已经满了");
}
arr[rear]=n;
rear=(rear+1)%maxsize;
}
public int getdata()
{
if(isempty())
{
System.out.println("队列为空");
}
int vale=arr[front];
front=(front+1)%maxsize;
return vale;
}
public int size()
{
return (rear-front+maxsize)%maxsize;
}
public void show() throws Exception {
if(isempty())
{
System.out.println("队列为空");
return;
}
for(int i=front;i<front+size();i++)
{
System.out.printf("arr[%d]=%d\n",i%maxsize,arr[i%maxsize]);
}
}
public int headqueue() throws Exception {
if(isempty())
{
throw new Exception("队列为空");
}
return arr[front];
}
}
环形队列
public class criclqueue {
int front;
int rear;
int maxsize;
int[] arr;
public criclqueue(int maxsize)
{
this.maxsize = maxsize;
front = 0;
rear = 0;
arr = new int[maxsize];
}
public void creatqueue(int length)
{
maxsize=length;
arr=new int[maxsize];
}
public boolean isfull()
{
return (rear+1)%maxsize==front;
}
public boolean isempty()
{
return front==rear;
}
public void adddata(int n)
{
if(isfull())
{
System.out.println("队列已经满了");
}
arr[rear]=n;
rear=(rear+1)%maxsize;
}
public int getdata()
{
if(isempty())
{
System.out.println("队列为空");
}
int vale=arr[front];
front=(front+1)%maxsize;
return vale;
}
public int size()
{
return (rear-front+maxsize)%maxsize;
}
public void show() throws Exception {
if(isempty())
{
System.out.println("队列为空");
return;
}
for(int i=front;i<front+size();i++)
{
System.out.printf("arr[%d]=%d\n",i%maxsize,arr[i%maxsize]);
}
}
public int headqueue() throws Exception {
if(isempty())
{
throw new Exception("队列为空");
}
return arr[front];
}
}
标签:arr,队列,int,maxsize,front,public,rear
From: https://www.cnblogs.com/jinnice/p/16834195.html