Last active
August 4, 2017 00:55
-
-
Save alexleventer/89176d727627b3ba6dc59c105939f1a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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