- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomQueue.java
More file actions
Latest commit
48 lines (40 loc) · 1.07 KB
/
Copy pathCustomQueue.java
File metadata and controls
48 lines (40 loc) · 1.07 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
packagecom.sarvesh.javabasics;
publicclassCustomQueue {
int[] arr;
intfront;
intrear;
publicCustomQueue(intsize) {
arr = newint[size];
front = 0;
rear = 0;
}
publicvoidenqueue(intx) {
if (rear == arr.length) {
System.out.println("The line is full! Cannot add " + x);
return;
}
arr[rear] = x;
rear++;
}
publicintdequeue() {
if (front == rear) {
System.out.println("The line is empty!");
return -1;
}
intservedPerson = arr[front];
front++;
returnservedPerson;
}
publicintpeek() {
if (front == rear) return -1;
returnarr[front];
}
publicstaticvoidmain(String[] args) {
CustomQueueengine = newCustomQueue(5);
engine.enqueue(10);
engine.enqueue(20);
engine.enqueue(30);
System.out.println("Served: " + engine.dequeue());
System.out.println("Next up: " + engine.peek());
}
}