// Java Program to Implement
// Queue Data Structure
class Queue {
private int[] arr;
private int front;
private int rear;
private int capacity;
private int size;
// Constructor to initialize the queue
public Queue(int capacity) {
[Link] = capacity;
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
// Insert an element at the rear of the queue
public void enqueue(int item) {
if (isFull()) {
[Link]("Queue is full");
return;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}
// Remove and return the element from the front of the queue
public int dequeue() {
if (isEmpty()) {
[Link]("Queue is empty");
return -1;
}
int removedItem = arr[front];
front = (front + 1) % capacity;
size--;
return removedItem;
}
// Return the element at the front of the queue without removing it
public int peek() {
if (isEmpty()) {
[Link]("Queue is empty");
return -1;
}
return arr[front];
}
// Check if the queue is empty
public boolean isEmpty() {
return size == 0;
}
// Check if the queue is full
public boolean isFull() {
return size == capacity;
}
}
public class Main {
public static void main(String[] args) {
Queue queue = new Queue(5);
[Link](10);
[Link](20);
[Link](30);
[Link]("Dequeued item: " + [Link]());
[Link]("Front item: " + [Link]());
[Link]("Is queue empty? " + [Link]());
}
}