Skip to content

Instantly share code, notes, and snippets.

@alexleventer
Last active August 4, 2017 00:55
Show Gist options
  • Save alexleventer/89176d727627b3ba6dc59c105939f1a0 to your computer and use it in GitHub Desktop.
Save alexleventer/89176d727627b3ba6dc59c105939f1a0 to your computer and use it in GitHub Desktop.
import java.util.*;
class ArrayQueue {
private String[] q;
private int front;
private int rear;
private int size;
ArrayQueue(int capacity) {
front = 0;
rear = 0;
size = 0;
}
public int capacity() {
return q.length;
}
public void enqueue(String s) {
if (size == q.length)
throw new QueueOverflowException();
else {
size++;
q[rear] = s;
rear++;
if (rear == q.length) rear = 0;
}
}
public String peek() {
if(empty())
throw new EmptyQueueException();
else
return q[front];
}
public String dequeue() {
if(empty())
throw new EmptyQueueException();
else {
size--;
String value = q[front];
q[front] = null;
front++;
if (front == q.length)
front = 0;
return value;
}
}
public boolean empty() {
return size == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment