Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java circular buffer implementation on array

// Java program to implement a
// Circular Buffer using an array
 
import java.io.*;
import java.lang.*;
class CircularBuffer {
 
    // Initial Capacity of Buffer
    private int capacity = 0;
    // Initial Size of Buffer
    private int size = 0;
    // Head pointer
    private int head = 0;
    // Tail pointer
    private int tail = -1;
    private Object[] array;
 
    // Constructor
    CircularBuffer(int capacity)
    {
        // Initializing the capacity of the array
        this.capacity = capacity;
 
        // Initializing the array
        array = new Object[capacity];
    }
 
    // Addition of elements
    public void add(Object element) throws Exception
    {
 
        // Calculating the index to add the element
        int index = (tail + 1) % capacity;
 
        // Size of the array increases as elements are added
        size++;
 
        // Checking if the array is full
        if (size == capacity) {
            throw new Exception("Buffer Overflow");
        }
 
        // Storing the element in the array
        array[index] = element;
 
        // Incrementing the tail pointer to point
        // to the element added currently
        tail++;
    }
 
    // Deletion of elements
    public Object get() throws Exception
    {
 
        // Checking if the array is empty
        if (size == 0) {
            throw new Exception("Empty Buffer");
        }
 
        // Calculating the index of the element to be
        // deleted
        int index = head % capacity;
 
        // Getting the element
        Object element = array[index];
 
        // Incrementing the head pointer to point
        // to the next element
        head++;
 
        // Decrementing the size of the array as the
        // elements are deleted
        size--;
 
        // Returning the first element
        return element;
    }
 
    // Retrieving the first element without deleting it
    public Object peek() throws Exception
    {
 
        // Checking if the array is empty
        if (size == 0) {
            throw new Exception("Empty Buffer");
        }
 
        // Calculating the index of the
        // element to be deleted
        int index = head % capacity;
 
        // Getting the element
        Object element = array[index];
 
        // Returning the element
        return element;
    }
 
    // Checking if the array is empty
    public boolean isEmpty() { return size == 0; }
 
    // Size of the array
    public int size() { return size; }
}
 
class Main {
    public static void main(String[] args) throws Exception
    {
 
        // Creating the Circular Buffer
        CircularBuffer cb = new CircularBuffer(10);
 
        // Adding elements to the circular Buffer
        cb.add(5);
        cb.add(6);
        cb.add(7);
        cb.add(1);
        cb.add(4);
 
        // Printing the elements
        System.out.println(
            "The elements are printed in the order :-");
        System.out.println(cb.get());
        System.out.println(cb.get());
        System.out.println(cb.get());
        System.out.println(cb.get());
        System.out.println(cb.get());
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: What is sleep() method 
Java :: clear array in java 
Java :: Recyclerview scramble after scrolling 
Java :: add days to a date java 
Java :: Which Is Better to Configure a Spring Boot Project — Properties or YAML? 
Java :: kotless scheduled event 
Java :: java class creation inside method 
Java :: cannot apply java lang integer android 
Java :: Java object of the file 
Java :: java :: operator 
Java :: how to create a 2d arraylist java 
Java :: java bufferedreader 
Java :: java break statement 
Java :: class syntax in java 
Java :: how to access methods from another class in java 
Java :: Java If ... Else 
Java :: nested for loop java 
Java :: string program to calculate number of characters in a string java 
Java :: biginteger modulo in java 
Java :: polimorfismo java ejemplo 
Java :: java game development course free 
Java :: Java StringBuilder filter same lines 
Sql :: delete mysql ubuntu 20.04 
Sql :: alter user mysql native password 
Sql :: sql server get utc date 
Sql :: restart the psql server windows 
Sql :: postgresql list extensions 
Sql :: how to get all tables in sql 
Sql :: date today snowflake 
Sql :: Found option without preceding group in config file: /etc/mysql/mariadb.conf.d/50-server.cnf at line: 1 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =