-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueArray.java
More file actions
52 lines (43 loc) · 986 Bytes
/
QueueArray.java
File metadata and controls
52 lines (43 loc) · 986 Bytes
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
public class QueueArray<T> {
// Attributes
private ArrayDynamic<T> queue;
private int size;
private int capacity;
// Constructors
public QueueArray() {
this.capacity = 10;
this.queue = new ArrayDynamic<T>(this.capacity);
this.size = 0;
}
public QueueArray(int capacity) {
this.capacity = capacity;
this.queue = new ArrayDynamic<T>(this.capacity);
this.size = 0;
}
// Methods
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void enqueue(T data) {
queue.set(size++, data);
}
public T dequeue() {
if (isEmpty()) {
return null;
}
size--;
return queue.remove(0);
}
public T peek() {
if (isEmpty()) {
return null;
}
return queue.get(0);
}
public String toString() {
return queue.toString();
}
}