forked from CI2692-AJ2022/interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQueue.java
More file actions
38 lines (32 loc) · 853 Bytes
/
MyQueue.java
File metadata and controls
38 lines (32 loc) · 853 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
import java.util.Arrays;
public class MyQueue implements MyQueueInterface{
public Employee[] queue;
public MyQueue() {
this.queue = new Employee[0];
}
@Override
public void add(Employee item) {
Employee[] newArray = new Employee[queue.length + 1];
newArray[queue.length] = item;
for(int i = 0; i < queue.length; i++) {
newArray[i] = queue[i];
}
queue = newArray;
}
@Override
public Employee peek() {
if(queue.length > 0) {
return queue[0];
}
return null;
}
@Override
public Employee poll() {
if(queue.length > 0) {
Employee valueToReturn = queue[0];
queue = Arrays.copyOfRange(queue, 1, queue.length);
return valueToReturn;
}
return null;
}
}