-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram105.java
More file actions
63 lines (50 loc) · 1.28 KB
/
Program105.java
File metadata and controls
63 lines (50 loc) · 1.28 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
62
63
class MyQueue {
// This class implements a queue using an array
int front, rear, size;
int capacity;
int[] arr;
MyQueue(int capacity) {
this.capacity = capacity;
front = 0;
size = 0;
rear = capacity - 1;
arr = new int[capacity];
}
void enqueue(int item) {
if (isFull()) {
return;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}
int dequeue() {
if (isEmpty()) {
return -1;
}
int item = arr[front];
front = (front + 1) % capacity;
size--;
return item;
}
boolean isFull() {
return size == capacity;
}
boolean isEmpty() {
return size == 0;
}
int peek() {
return arr[front];
}
}
public class Program105 {
public static void main(String[] args) {
MyQueue queue = new MyQueue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println("Front element: " + queue.peek()); // 10
System.out.println("Removed: " + queue.dequeue()); // 10
System.out.println("Is queue empty? " + queue.isEmpty()); // false
}
}