-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayQueue.java
59 lines (49 loc) · 1.27 KB
/
ArrayQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
@Author: mohammed.shalan
@Date: 14-Jul-22
*/
import Exceptions.EmptyQueueException;
import Exceptions.FullQueueException;
import Interfaces.Queue;
public class ArrayQueue<T> implements Queue<T> {
private static int CAPACITY = 1000;
private int size;
private int front = 0;
private int next = 0;
private T[] array;
public ArrayQueue() {
this(CAPACITY);
}
public ArrayQueue(int size) {
this.size = size;
array = (T[]) new Object[size];
}
@Override
public int size() {
return (isEmpty()) ? 0 : next-front;
}
@Override
public boolean isEmpty() {
return next==front;
}
@Override
public T getFront() throws EmptyQueueException {
if (isEmpty())
throw new EmptyQueueException("The Queue is Empty!");
return array[front];
}
@Override
public void enqueue(T element) {
if (next < array.length) {
array[next++] = element;
} else {
throw new FullQueueException("The Queue is full!");
}
}
@Override
public T dequeue() throws EmptyQueueException {
if (isEmpty() || front == next)
throw new EmptyQueueException("The Queue is empty!");
return array[front++];
}
}