Я публикую здесь полный код для реализации циклической очереди с использованием массива в Java
trim (): подрезать размер массива.
package com.java.util.collection.advance.datastructure.queue;
public interface Queue<E>{
boolean addR(E e);
E removeL();
E element();
boolean isFull();
boolean isEmpty();
void trim();
}
package com.java.util.collection.advance.datastructure.queue;
public interface CircularQueue<E> extends Queue<E>{
}
package com.java.util.collection.advance.datastructure.queue;
import java.util.Arrays;
public class MyCircularQueue<E> implements CircularQueue<E>{
private int front = 0;
private int rear =-1;
private E[] elements =null;
private static final int DEFAULT_INTIAL_CAPACITY =100;
private int size =0;
public MyCircularQueue(){
this(DEFAULT_INTIAL_CAPACITY);
}
@SuppressWarnings("unchecked")
public MyCircularQueue(int intialCapacity){
if(intialCapacity < 0){
throw new IllegalArgumentException("intial capacity can't be null");
}
elements =(E[]) new Object[intialCapacity];
}
@Override
public boolean addR(E e) {
if(! isFull()) {
rear = (rear+1)%elements.length;
elements[rear] = e;
size++;
return true;
}
return false;
}
@Override
public E removeL() {
E element =null;
if(!isEmpty()){
if(front == elements.length-1)
{
front =(front+1)%elements.length;
}
element=elements[front];
// Nullify the reference
elements[front] =null;
front++;
--size;
}
return element;
}
@Override
public E element() {
E element =null;
if(!isEmpty()){
element=elements[front];
}
return element;
}
@Override
public boolean isFull() {
return size == elements.length;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
// Do Nothing
public void trim() {
@SuppressWarnings("unchecked")
E[]dest =(E[]) new Object[size];
if(front < rear) {
System.arraycopy(elements, front, dest, front-1,rear);
} else {
System.arraycopy(elements, front, dest, 0, size-front+1);
System.arraycopy(elements, 0, dest, size-front+1, front-rear);
front =0;
rear = size;
}
elements =dest;
}
@Override
public String toString() {
return "MyCircularQueue [front=" + front + ", rear=" + rear
+ ", elements=" + Arrays.toString(elements) + ", size=" + size
+ "]";
}
}
Тестовый класс:
package com.java.util.collection.advance.datastructure.queue;
import java.util.Random;
public class MyCircularQueueApp<E> {
public static void main(String[] args) {
CircularQueue<Integer> cirQueue =new MyCircularQueue<Integer>(11);
Random random =new Random();
for(int i=0;i<10;i++){
cirQueue.addR(random.nextInt(3));
}
System.out.println(cirQueue);
cirQueue.removeL();
System.out.println("Before triming: "+cirQueue);
cirQueue.trim();
System.out.println("After triming: "+cirQueue);
cirQueue.removeL();
System.out.println(cirQueue);
cirQueue.addR(1000);
System.out.println(cirQueue);
cirQueue.addR(10000);
cirQueue.addR(100000);
System.out.println(cirQueue);
System.out.println(cirQueue.element());
}
}
Выход:
MyCircularQueue [front=0, rear=9, elements=[1, 2, 2, 2, 1, 2, 2, 1, 2, 1, null], size=10]
Before triming: MyCircularQueue [front=1, rear=9, elements=[null, 2, 2, 2, 1, 2, 2, 1, 2, 1, null], size=9]
After triming: MyCircularQueue [front=1, rear=9, elements=[2, 2, 2, 1, 2, 2, 1, 2, 1], size=9]
MyCircularQueue [front=2, rear=9, elements=[2, null, 2, 1, 2, 2, 1, 2, 1], size=8]
MyCircularQueue [front=2, rear=1, elements=[2, 1000, 2, 1, 2, 2, 1, 2, 1], size=9]
MyCircularQueue [front=2, rear=1, elements=[2, 1000, 2, 1, 2, 2, 1, 2, 1], size=9]