Parallel Programming WS 2014/2015 - File Queue.java
import java.util.NoSuchElementException;
public class Queue {
private int capacity;
private int elemCount;
private int firstIndex;
private int[] elems;
public Queue(int n) {
capacity = n;
elemCount = 0;
firstIndex = 0;
elems = new int[n];
}
public void enqueue(int elem) {
if (elemCount == capacity) {
throw new IllegalStateException();
}
int index = firstIndex + elemCount;
if (index >= capacity) {
index -= capacity;
}
elems[index] = elem;
elemCount += 1;
}
public int dequeue() {
if (elemCount == 0) {
throw new IllegalStateException();
}
int res = elems[firstIndex];
firstIndex += 1;
if (firstIndex == capacity) {
firstIndex = 0;
}
elemCount -= 1;
return res;
}
public int getElementCount() {
return elemCount;
}
public boolean isFull() {
return elemCount == capacity;
}
public boolean isEmpty() {
return elemCount == 0;
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 20.11.2014


