Java Program to implement Circular Buffer

This is a Java Program to implement Circular Buffer. A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

Here is the source code of the Java Program to implement Circular Buffer. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to implement Circular Buffer
 **/
 
 import java.util.Scanner;
 
/** Class Circular Buffer **/
class CircularBuffer
{
    private int maxSize;
    private int front = 0;  
    private int rear = 0;  
    private int bufLen = 0;  
    private char[] buf;    
 
    /** constructor **/
    public CircularBuffer(int size) 
    {
        maxSize = size;
        front = rear = 0;
        rear = 0;
        bufLen = 0;
        buf = new char[maxSize];
    }
    /** function to get size of buffer **/
    public int getSize()
    {
        return bufLen;
    }
    /** function to clear buffer **/
    public void clear()
    {
        front = rear = 0;
        rear = 0;
        bufLen = 0;
        buf = new char[maxSize];
    }
    /** function to check if buffer is empty **/
    public boolean isEmpty() 
    {
        return bufLen == 0;
    }
    /** function to check if buffer is full **/
    public boolean isFull() 
    {
        return bufLen == maxSize;
    } 
    /** insert an element **/
    public void insert(char c) 
    {
        if (!isFull() ) 
        {
            bufLen++;
            rear = (rear + 1) % maxSize;
            buf[rear] = c;
        }
        else
            System.out.println("Error : Underflow Exception");
    }
    /** delete an element **/
    public char delete() 
    {
        if (!isEmpty() ) 
        {
            bufLen--;
            front = (front + 1) % maxSize;
            return buf[front];
        }
        else 
        {
            System.out.println("Error : Underflow Exception");
            return ' ';
        }
    }       
    /** function to print buffer **/
    public void display() 
    {
        System.out.print("\nBuffer : ");
        for (int i = 0; i < maxSize; i++)
            System.out.print(buf[i] +" ");
        System.out.println();    
    }
}
 
 
/** Class CircularBufferTest  **/
public class CircularBufferTest
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
 
        System.out.println("Circular Buffer Test\n");
        System.out.println("Enter Size of Buffer ");
        int n = scan.nextInt();
        /* creating object of class CircularBuffer */
        CircularBuffer cb = new CircularBuffer(n); 
 
        /* Perform Circular Buffer Operations */        
        char ch;
 
        do
        {
            System.out.println("\nCircular Buffer Operations");
            System.out.println("1. insert");
            System.out.println("2. remove");
            System.out.println("3. size");
            System.out.println("4. check empty");
            System.out.println("5. check full");
            System.out.println("6. clear");
 
            int choice = scan.nextInt();
            switch (choice)
            {
            case 1 : 
                System.out.println("Enter character to insert");
                cb.insert( scan.next().charAt(0) );                                        
                break;                         
            case 2 : 
                System.out.println("Removed Element = "+ cb.delete());
                break;                         
            case 3 : 
                System.out.println("Size = "+ cb.getSize());
                break;                            
            case 4 : 
                System.out.println("Empty status = "+ cb.isEmpty());
                break;                
            case 5 : 
                System.out.println("Full status = "+ cb.isFull());
                break; 
            case 6 : 
                System.out.println("\nBuffer Cleared\n");
                cb.clear();
                break;                                    
            default : System.out.println("Wrong Entry \n ");
                break;
            }
            /* display Buffer */
            cb.display();     
 
            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);
 
        } while (ch == 'Y'|| ch == 'y');                                                        
    }    
}
Circular Buffer Test
 
Enter Size of Buffer
5
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
a
 
Buffer :   a
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
b
 
Buffer :   a b
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
c
 
Buffer :   a b c
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
d
 
Buffer :   a b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
e
 
Buffer : e a b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
f
Error : Underflow Exception
 
Buffer : e a b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
2
Removed Element = a
 
Buffer : e a b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
1
Enter character to insert
f
 
Buffer : e f b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
3
Size = 5
 
Buffer : e f b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
5
Full status = true
 
Buffer : e f b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
2
Removed Element = b
 
Buffer : e f b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
3
Size = 4
 
Buffer : e f b c d
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
6
 
Buffer Cleared
 
 
Buffer :
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
3
Size = 0
 
Buffer :
 
Do you want to continue (Type y or n)
 
y
 
Circular Buffer Operations
1. insert
2. remove
3. size
4. check empty
5. check full
6. clear
4
Empty status = true
 
Buffer :
 
Do you want to continue (Type y or n)
 
n

Related posts:

Spring Boot: Customize Whitelabel Error Page
Java Program to Solve a Matching Problem for a Given Specific Case
Các kiểu dữ liệu trong java
Java Program to Construct a Random Graph by the Method of Random Edge Selection
A Quick Guide to Spring MVC Matrix Variables
Spring REST with a Zuul Proxy
Spring Security Registration – Resend Verification Email
A Guide to JUnit 5 Extensions
Java Program to Find Minimum Element in an Array using Linear Search
“Stream has already been operated upon or closed” Exception in Java
Checking for Empty or Blank Strings in Java
Converting String to Stream of chars
Introduction to Spring Boot CLI
Java Program to Permute All Letters of an Input String
How to Return 404 with Spring WebFlux
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement PriorityQueue API
Convert a Map to an Array, List or Set in Java
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Check whether Graph is a Bipartite using DFS
Java InputStream to String
Java Program to Implement Hash Trie
How to Change the Default Port in Spring Boot
Semaphore trong Java
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
List Interface trong Java