public class MyQueue {
private long[] arr;
private int elements;
private int front;
private int end;
public static void main(String[] args) {
MyQueue mq = new MyQueue(4);
mq.insert(23);
mq.insert(45);
mq.insert(32);
mq.insert(24);
System.out.println(mq.isEmpty());
System.out.println(mq.isFull());
System.out.println(mq.peek());
while(!mq.isEmpty()){
System.out.print(mq.remove() +",");
}
}
public MyQueue(){
arr = new long [10];
elements = 0;
front = 0 ;
end = -1;
}
public MyQueue(int maxsize){
arr = new long [maxsize];
elements = 0;
front = 0 ;
end = -1;
}
public void insert (long value){
if(end == arr.length -1 ){
end = -1 ;
}
arr[++end] = value;
elements++;
}
public long remove(){
long value = arr[front++];
if(front == arr.length ){
front = 0 ;
}
elements--;
return value;
}
public long peek(){
return arr[front];
}
public boolean isEmpty(){
return elements == 0;
}
public boolean isFull(){
return elements == arr.length;
}
}
java实现循环列队
最新推荐文章于 2024-08-02 11:24:06 发布