Open In App

Array with Constant Time Insertions and Deletions in Java

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Circular Buffer is a kind of data structure that allows for insertions and deletions at both the front and back ends of the buffer in constant time. It does this by arranging the components in a circle.

Constant Time Insertions and Deletions of Array

Keeping two pointers front and rear and using the modulo operation to wrap around the buffer as needed are the fundamental concepts. To make sure an element remains within the buffer's boundaries when it is inserted or deleted, you relocate the corresponding pointer and update its location using the modulo operation.

Implementation:

Java
// Use of Circular Buffer
public class CircularBuffer {

    // Array to store elements
    private int[] buffer;
    private int front;
	private int rear;
	private int size;

    // Constructor to initialize the CircularBuffer with a
    // given capacity
    public CircularBuffer(int capacity)
    {
        // Initialize the buffer
        // array with the
        // specified capacity
        buffer = new int[capacity];

        // Initialize front to -1 (indicating an
        // empty buffer)
        front = -1;

        // Initialize rear to -1 (indicating an
        // empty buffer)
        rear = -1;

        // Initialize size to 0 (indicating an
        // empty buffer)
        size = 0;
    }

    // Method to check if the buffer is empty
    public boolean isEmpty()
    {
        return size == 0;
    }

    // Method to check if the buffer is full
    public boolean isFull()
    {
        return size == buffer.length;
    }

    // Method to insert an element at the start of the
    // buffer
    public void insertAtStart(int value)
    {
        // Check if the buffer is full
        if (isFull()) {
            System.out.println("Buffer is full");
            return;
        }

        // If buffer is empty, set front
        // and rear to 0
        if (isEmpty()) {
            front = 0;
            rear = 0;
        }

        // Otherwise, calculate the new front index
        // using modulo arithmetic
        else {
            front = (front - 1 + buffer.length)
                    % buffer.length;
        }

        buffer[front] = value;
        size++;
    }

    // Method to insert an element at the end of the buffer
    public void insertAtEnd(int value)
    {
        // Check if the buffer is full
        if (isFull()) {
            System.out.println("Buffer is full");
            return;
        }

        // If buffer is empty, set front
        // and rear to 0
        if (isEmpty()) {
            front = 0;
            rear = 0;
        }

        // Otherwise, calculate the new rear index
        // using modulo arithmetic
        else {
            rear = (rear + 1) % buffer.length;
        }

        // Insert the value at the rear
        buffer[rear] = value;

        // Increment the size
        size++;
    }

    // Method to delete an element from the start of the
    // buffer
    public void deleteFromStart()
    {
        if (isEmpty()) { // Check if the buffer is empty
            System.out.println("Buffer is empty");
            return;
        }

        // If buffer has only one element,
        // reset front and rear
        if (size == 1) {
            front = -1;
            rear = -1;
        }
        // Otherwise, calculate the new front index
        // using modulo arithmetic
        else {
            front = (front + 1) % buffer.length;
        }

        // Decrement the size
        size--;
    }

    // Method to delete an element from the end of the
    // buffer
    public void deleteFromEnd()
    {
        // Check if the buffer is empty
        if (isEmpty()) {
            System.out.println("Buffer is empty");
            return;
        }

        // If buffer has only one element,
        // reset front and rear
        if (size == 1) {
            front = -1;
            rear = -1;
        }

        // Otherwise, calculate the new rear index
        // using modulo arithmetic
        else {
            rear = (rear - 1 + buffer.length)
                   % buffer.length;
        }
      	
        size--;
    }

    // Method to display the contents of the buffer
    public void display()
    {
        // Check if the buffer is empty
        if (isEmpty()) {
            System.out.println("Circular Buffer is empty.");
            return;
        }

        System.out.print("Circular Buffer: ");
        int count = 0;
        int index = front;

      	// Iterate over the buffer
        // and print its elements
        while (count < size) {
            System.out.print(buffer[index] + " ");
            index = (index + 1) % buffer.length;
            count++;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        CircularBuffer c = new CircularBuffer(5);

        c.insertAtEnd(1);
        c.insertAtEnd(2);
        c.insertAtEnd(3);

        // Output: Circular Buffer: 1 2 3
        c.display();

        c.deleteFromStart();

        // Output: Circular Buffer: 2 3
        c.display();

        c.insertAtStart(4);
        c.insertAtEnd(5);

        // Output: Circular Buffer: 4 2 3 5
        c.display();

        c.deleteFromEnd();

        // Output: Circular Buffer: 4 2 3
        c.display();
    }
}

Output
Circular Buffer: 1 2 3 
Circular Buffer: 2 3 
Circular Buffer: 4 2 3 5 
Circular Buffer: 4 2 3 

Practice Tags :

Similar Reads