Open In App

Array with Constant Time Insertions and Deletions in Java

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

Prerequisites

Java Program to Implement 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.

Below is the implementation of Constant Time Insertions and Deletions of Array:

Java




// Java Program to Implement Constant
// Time Insertions and Deletions of Array
// Use of Circular Buffer
public class CircularBuffer {
  
    // Array to store elements
    private int[] buffer;
  
    // Index of the front element
    private int front;
  
    // Index of the rear element
    private int rear;
  
    // Number of elements currently in the buffer
    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; // The buffer is empty if the size is 0
    }
  
    // Method to check if the buffer is full
    public boolean isFull()
    {
        // The buffer is full if the
        // size equals the capacity
        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. Cannot insert.");
            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;
        }
  
        // Insert the value at the front
        buffer[front] = value;
  
        // Increment the size
        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. Cannot insert.");
            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. Cannot delete.");
            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. Cannot delete.");
            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;
        }
        // Decrement the size
        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();
    }
  
    // Main method to test the CircularBuffer implementation
    public static void main(String[] args)
    {
        CircularBuffer circularBuffer
            = new CircularBuffer(5);
  
        circularBuffer.insertAtEnd(1);
        circularBuffer.insertAtEnd(2);
        circularBuffer.insertAtEnd(3);
  
        // Output: Circular Buffer: 1 2 3
        circularBuffer.display();
  
        circularBuffer.deleteFromStart();
  
        // Output: Circular Buffer: 2 3
        circularBuffer.display();
  
        circularBuffer.insertAtStart(4);
        circularBuffer.insertAtEnd(5);
  
        // Output: Circular Buffer: 4 2 3 5
        circularBuffer.display();
  
        circularBuffer.deleteFromEnd();
  
        // Output: Circular Buffer: 4 2 3
        circularBuffer.display();
    }
}


Output

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




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads