/
alexShibanov
/
556677
Обзор
Документация
Войти
/
alexShibanov
/
556677
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
Circular_queue.java
75 строк
2 KB
Sirsha Dey
Create Circular_queue.java
22 окт 2023, 16:09
Не верифицирован
22 окт 2023, 16:09
1446162
Код
Авторство
О чём код?
public class CircularQueue { private int[] arr; private int front; private int rear; private int capacity; private int size; public CircularQueue(int capacity) { this.capacity = capacity; this.arr = new int[capacity]; this.front = -1; this.rear = -1; this.size = 0; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == capacity; } public void enqueue(int element) { if (isFull()) { System.out.println("Queue is full. Cannot enqueue more elements."); return; } if (isEmpty()) { front = 0; } rear = (rear + 1) % capacity; arr[rear] = element; size++; } public int dequeue() { if (isEmpty()) { System.out.println("Queue is empty. Cannot dequeue elements."); return -1; } int element = arr[front]; if (front == rear) { front = -1; rear = -1; } else { front = (front + 1) % capacity; } size--; return element; } public int front() { if (isEmpty()) { System.out.println("Queue is empty. No front element."); return -1; } return arr[front]; } } public static void main(String[] args) { CircularQueue queue = new CircularQueue(5); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); System.out.println(queue.dequeue()); // Output: 1 System.out.println(queue.front()); // Output: 2 }