-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomQueue.java
More file actions
61 lines (48 loc) · 1.2 KB
/
CustomQueue.java
File metadata and controls
61 lines (48 loc) · 1.2 KB
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
60
61
public class CustomQueue {
private int[] data;
private static final int DEFAULT_SIZE = 10;
int end = 0;
public CustomQueue(){
this(DEFAULT_SIZE);
}
public CustomQueue(int size) {
this.data = new int[size];
}
public boolean isFull() {
return end == data.length; // ptr is at last index
}
public boolean isEmpty() {
return end == 0;
}
public boolean insert(int item) {
if (isFull()) {
return false;
}
data[end++] = item;
return true;
}
public int remove() throws Exception {
if (isEmpty()) {
throw new Exception("Queue is empty");
}
int removed = data[0];
// shift the elements to left
for (int i = 1; i < end; i++) {
data[i-1] = data[i];
}
end--;
return removed;
}
public int front() throws Exception{
if (isEmpty()) {
throw new Exception("Queue is empty");
}
return data[0];
}
public void display() {
for (int i = 0; i < end; i++) {
System.out.print(data[i] + " <- ");
}
System.out.println("END");
}
}