Java:
public class Queue<Type> implements Iterable<Type>{
private static final int DEFAULT_SIZE = 7;
private int writeCursor;
private int readCursor;
private int actualSize;
private Type[] buffer;
private int maxSize;
// Konstruktoren
public Queue() throws IllegalQueueSize {
this(DEFAULT_SIZE);
}
public Queue(int maxSize) throws IllegalQueueSize {
if (maxSize <= 0)
throw new IllegalQueueSize();
this.maxSize = maxSize;
this.buffer = (Type[]) new Object[maxSize];
writeCursor = 0;
readCursor = 0;
actualSize = 0;
maxSize = 0;
}
// Getter/Setter
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public int getWriteCursor() {
return writeCursor;
}
public void setWirteCursor(int wirteCursor) {
this.writeCursor = wirteCursor;
}
public int getReadCursor() {
return readCursor;
}
public void setReadCursor(int readCursor) {
this.readCursor = readCursor;
}
public int getActualSize() {
return actualSize;
}
public void setActualSize(int actualSize) {
this.actualSize = actualSize;
}
public Type[] getBuffer() {
return buffer;
}
public void setBuffer(Type[] buffer) {
this.buffer = buffer;
}
public static int getDefaultSize() {
return DEFAULT_SIZE;
}
// --------------------METHODEN--------------------------------------//
public boolean isQueueFull() {
if (actualSize == maxSize) {
return true;
} else {
return false;
}
}
public boolean isQueueEmpty() {
if (actualSize == 0) {
return true;
} else {
return false;
}
}
public int actualSize() throws QueueIsEmpty {
if (isQueueEmpty() == true)
throw new QueueIsEmpty();
if (actualSize > maxSize) {
actualSize = maxSize;
return actualSize;
}
return actualSize;
}
public Type readValue(int index) throws QueueIsEmpty, IllegalIndex {
if (isQueueEmpty() == true)
throw new QueueIsEmpty();
if (index > buffer.length) {
throw new IllegalIndex();
} else {
return buffer[index];
}
}
public void insert(Type value) {
if (actualSize == buffer.length) {
System.out.println("Overflow");
}
buffer[writeCursor] = value;
writeCursor = (writeCursor + 1) % buffer.length;
actualSize++;
}
public Type removeLastElement() throws QueueIsEmpty {
if (isQueueEmpty() == true)
throw new QueueIsEmpty();
Type value = buffer[0];
buffer[0] = null;
actualSize--;
return value;
}
public void removeAll() {
for (int i = 0; i < buffer.length; i++)
buffer[i] = null;
actualSize = 0;
}
public String toString() {
String string = "";
for (int i = 0; i < buffer.length; i++)
string += "-" + buffer[i] + "-";
return string;
}
@Override
public Iterator<Type> iterator() {
return null;
}
}