-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
68 lines (60 loc) · 1.69 KB
/
Copy pathQueue.java
File metadata and controls
68 lines (60 loc) · 1.69 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
64
65
66
67
68
package model;
public class Queue {
private int[] array;
private int front;
private int rear;
private int size;
private int capacity;
public Queue() {
this.capacity = 10; // Initial capacity
this.array = new int[capacity];
this.front = 0;
this.rear = -1;
this.size = 0;
}
public void enqueue(int bookingId) {
if (size == capacity) {
resize();
}
rear = (rear + 1) % capacity;
array[rear] = bookingId;
size++;
System.out.println("Enqueued booking ID: " + bookingId);
}
public Integer dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return null;
}
int bookingId = array[front];
front = (front + 1) % capacity;
size--;
System.out.println("Dequeued booking ID: " + bookingId);
return bookingId;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public int[] getQueue() {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = array[(front + i) % capacity];
}
return result;
}
private void resize() {
int newCapacity = capacity * 2;
int[] newArray = new int[newCapacity];
for (int i = 0; i < size; i++) {
newArray[i] = array[(front + i) % capacity];
}
array = newArray;
front = 0;
rear = size - 1;
capacity = newCapacity;
System.out.println("Resized queue to capacity: " + capacity);
}
}