-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayQueue.java
57 lines (46 loc) · 979 Bytes
/
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
/**
* Created by jkeys on 10/14/2015.
*/
import java.util.*;
public class ArrayQueue<T> implements QueueInterface<T> {
private int front;
private int back;
private int size;
private ArrayList<T> data;
public ArrayQueue() {
size = 0;
front = back = -1;
data = new ArrayList<T>();
}
public void enqueue(T e) {
if(size == 0) {
data.add(e);
front = back = 0;
size++;
return;
}
back++;
data.add(back, e);
size++;
}
public T dequeue() {
if(size == 0)
return null;
T result = data.get(0);
data.remove(0);
back--;
size--;
return result;
}
public T front() {
if (size == 0)
return null;
return data.get(0);
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
}