Implementing a Circular Queue in Java

Implement Circular Queue (LinkedList Version) class CircularQueue { int[] arr; int front, rear, size, capacity; CircularQueue(int cap) { capacity = cap; arr = new int[cap]; front = 0; rear = -1; size = 0; } boolean isEmpty() { return size == 0; } boolean isFull() { return size == capacity; } void enqueue(int x) { if (isFull()) { System.out.println("Overflow"); return; } rear = (rear + 1) % capacity; arr[rear] = x; size++; } int dequeue() { if (isEmpty()) { System.out.println("Underflow"); return -1; } int val = arr[front]; front = (front + 1) % capacity; size--; return val; } } #DSA #DataStructuresAndAlgorithms #Coding #Programmer #LeetCode #CodeEveryday #JavaDSA #CodingPractice #ProblemSolving #CP #CompetitiveProgramming #DailyCoding #TechJourney #CodingCommunity #DeveloperLife #100DaysOfCode #CodeWithMe #LearnToCode #GeekForGeeks #CodingMotivation

To view or add a comment, sign in

Explore content categories