forked from Anjalijha12345/DSA--Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomQueue.java
68 lines (52 loc) · 1.38 KB
/
CustomQueue.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
60
61
62
63
64
65
66
67
68
package saikat;
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 void display(){
for (int i = 0; i < end; i++) {
System.out.print(data[i] + " <- ");
}
System.out.println("End");
}
public static void main(String[] args) {
CustomQueue queue = new CustomQueue(9);
queue.insert(8);
queue.insert(9);
queue.insert(12);
queue.insert(44);
queue.insert(80);
queue.display();
}
}