-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomQueue.java
More file actions
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
package com.sarvesh.javabasics;
public class CustomQueue {
int[] arr;
int front;
int rear;
public CustomQueue(int size) {
arr = new int[size];
front = 0;
rear = 0;
}
public void enqueue(int x) {
if (rear == arr.length) {
System.out.println("The line is full! Cannot add " + x);
return;
}
arr[rear] = x;
rear++;
}
public int dequeue() {
if (front == rear) {
System.out.println("The line is empty!");
return -1;
}
int servedPerson = arr[front];
front++;
return servedPerson;
}
public int peek() {
if (front == rear) return -1;
return arr[front];
}
public static void main(String[] args) {
CustomQueue engine = new CustomQueue(5);
engine.enqueue(10);
engine.enqueue(20);
engine.enqueue(30);
System.out.println("Served: " + engine.dequeue());
System.out.println("Next up: " + engine.peek());
}
}