CircularArrayQueueを実装しようとしています。私は待ち行列が通過しなければならないJUnitテストを受けました。私は、前と後ろのポインタで何か間違っていると思います。私はデータ構造とアルゴリズムの学習にどのようにアプローチすべきですか?CircularArrayQueue実装Java
import java.util.NoSuchElementException;
public class CircularArrayQueue implements MyQueue {
private Integer[] array;
// initial size of the array
private int N;
private int front;
private int rear;
public CircularArrayQueue() {
this.N = 10;
array = new Integer[N];
front = rear = 0;
}
public CircularArrayQueue(int size) {
this.N = size;
array = new Integer[N];
front = rear = 0;
}
// enqueues an element at the rear of the queue
// if the queue is already full it is resized, doubling its size
@Override
public void enqueue(int in) {
if (rear == N) {
if (front == 0) {
resize();
array[rear] = in;
rear++;
} else {
array[rear] = in;
rear = 0;
}
} else {
array[rear] = in;
rear++;
}
}
public void resize() {
Integer[] temp = new Integer[array.length * 2];
for (int i = 0; i < array.length; i++) {
temp[i] = array[i];
}
temp = array;
}
// dequeues an element
// if the queue is empty a NoSuchElement Exception is thrown
@Override
public int dequeue() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("The queue is full");
}
int headElement = array[front];
if (front == N) {
array[front] = null;
front = 0;
} else {
array[front] = null;
front++;
}
return headElement;
}
@Override
public int noItems() {
return N - getCapacityLeft();
}
@Override
public boolean isEmpty() {
return (getCapacityLeft() == N);
}
// return the number of indexes that are empty
public int getCapacityLeft() {
return (N - rear + front) % N;
}
}
私たちはあなたのためにあなたのコードをデバッグしないでください。ヒント:少なくとも失敗したテストのソースを提供してください。 – GhostCat
私はちょうど私のエンキューとデキューの実装のフィードバックをしたいです。それはあなたのために難しい場合は問題はありません。 –
@RadoslavTodorovフィードバックが必要な場合は、コードにいくつかの問題があります。 –