Skip to content

Instantly share code, notes, and snippets.

@secretdataz
Last active January 17, 2019 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save secretdataz/0da118ed7e59c24f79ee1012a98d133c to your computer and use it in GitHub Desktop.
Save secretdataz/0da118ed7e59c24f79ee1012a98d133c to your computer and use it in GitHub Desktop.
[Java] Circular array queue implementation
/**
* Circular array queue implementation
* @author Jittapan Pluemsumran
*/
public class CSQueue<T> {
T[] data;
int first = -1;
int last = -1;
int size; // for quick access
public CSQueue(int size) {
this.size = size;
data = (T[])new Object[size];
}
public boolean isEmpty() {
return first == -1 && last == -1;
}
public boolean isFull() {
return (last + 1)%size == first;
}
public void enqueue(T el) throws IllegalStateException {
if(isFull()) {
throw new IllegalStateException("Queue is full.");
}
if(isEmpty()){
first = last = 0;
} else {
last = (last + 1)%size;
}
data[last] = el;
}
public T dequeue() throws IllegalStateException {
T tmp;
if(isEmpty()) {
throw new IllegalStateException("Queue is full.");
}
if(first == last) {
tmp = data[first];
first = last = -1;
} else {
tmp = data[first];
first = (first +1)%size;
}
return tmp;
}
public T getFirst() throws IllegalStateException {
if(isEmpty()) {
throw new IllegalStateException("Queue is empty.");
}
return data[first];
}
public T getLast() throws IllegalStateException {
if(isEmpty()) {
throw new IllegalStateException("Queue is empty.");
}
return data[last];
}
public String toString() {
int count = (last +size- first)%size + 1;
StringBuilder output = new StringBuilder("[");
for(int i = 0; i <count; i++) {
int index = (first + i) % size;
output.append(data[index]).append(", ");
}
return output.toString().trim() + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment