java学习经验总结------循环队列的实现(数组)


package datastructure;/*数组实现循环队列
队列first in first out*/
public class CircularQueue { public static void main(String[] args) {
// TODO 自动生成的方法存根
Circular_Queue cq = new Circular_Queue(3);
cq.add("1");
cq.add("2");
cq.add("3");
cq.delete();
cq.delete();
cq.print();
} }
class Circular_Queue{
private int front;
private int rear;
private int maxsize;
private int count=0;
private String a[];
public Circular_Queue(int max) {
a=new String[max];
this.front=0;
this.rear=0;
this.maxsize=max;
}
public void add(String value){//添加元素 a[(this.rear+this.maxsize+1)%(this.maxsize+1)]=value;
this.rear=(++this.rear+this.maxsize+1)%(this.maxsize+1);
this.count++; }
public int Count() {//加入元素的个数
return this.count;
}
public void delete() {
a[(this.front+this.maxsize+1)%(this.maxsize+1)]=null;
this.front=(++this.front+this.maxsize+1)%(this.maxsize+1);
this.count--;
if(front==rear)
System.out.println("队列已空");
}
public void print() {//打印输出列表
int i=0;
while(i<count) {
System.out.println(a[front+i]);
i++;
}
if((this.rear+this.maxsize+2)%(this.maxsize+1)==this.front)
System.out.println("队列已满!!!");
}
}
05-16 08:44