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.

  1. /**
  2.  ** Java Program to implement Circular Buffer
  3.  **/
  4.  
  5.  import java.util.Scanner;
  6.  
  7. /** Class Circular Buffer **/
  8. class CircularBuffer
  9. {
  10.     private int maxSize;
  11.     private int front = 0;  
  12.     private int rear = 0;  
  13.     private int bufLen = 0;  
  14.     private char[] buf;    
  15.  
  16.     /** constructor **/
  17.     public CircularBuffer(int size) 
  18.     {
  19.         maxSize = size;
  20.         front = rear = 0;
  21.         rear = 0;
  22.         bufLen = 0;
  23.         buf = new char[maxSize];
  24.     }
  25.     /** function to get size of buffer **/
  26.     public int getSize()
  27.     {
  28.         return bufLen;
  29.     }
  30.     /** function to clear buffer **/
  31.     public void clear()
  32.     {
  33.         front = rear = 0;
  34.         rear = 0;
  35.         bufLen = 0;
  36.         buf = new char[maxSize];
  37.     }
  38.     /** function to check if buffer is empty **/
  39.     public boolean isEmpty() 
  40.     {
  41.         return bufLen == 0;
  42.     }
  43.     /** function to check if buffer is full **/
  44.     public boolean isFull() 
  45.     {
  46.         return bufLen == maxSize;
  47.     } 
  48.     /** insert an element **/
  49.     public void insert(char c) 
  50.     {
  51.         if (!isFull() ) 
  52.         {
  53.             bufLen++;
  54.             rear = (rear + 1) % maxSize;
  55.             buf[rear] = c;
  56.         }
  57.         else
  58.             System.out.println("Error : Underflow Exception");
  59.     }
  60.     /** delete an element **/
  61.     public char delete() 
  62.     {
  63.         if (!isEmpty() ) 
  64.         {
  65.             bufLen--;
  66.             front = (front + 1) % maxSize;
  67.             return buf[front];
  68.         }
  69.         else 
  70.         {
  71.             System.out.println("Error : Underflow Exception");
  72.             return ' ';
  73.         }
  74.     }       
  75.     /** function to print buffer **/
  76.     public void display() 
  77.     {
  78.         System.out.print("\nBuffer : ");
  79.         for (int i = 0; i < maxSize; i++)
  80.             System.out.print(buf[i] +" ");
  81.         System.out.println();    
  82.     }
  83. }
  84.  
  85.  
  86. /** Class CircularBufferTest  **/
  87. public class CircularBufferTest
  88. {
  89.     public static void main(String[] args)
  90.     {
  91.         Scanner scan = new Scanner(System.in);
  92.  
  93.         System.out.println("Circular Buffer Test\n");
  94.         System.out.println("Enter Size of Buffer ");
  95.         int n = scan.nextInt();
  96.         /* creating object of class CircularBuffer */
  97.         CircularBuffer cb = new CircularBuffer(n); 
  98.  
  99.         /* Perform Circular Buffer Operations */        
  100.         char ch;
  101.  
  102.         do
  103.         {
  104.             System.out.println("\nCircular Buffer Operations");
  105.             System.out.println("1. insert");
  106.             System.out.println("2. remove");
  107.             System.out.println("3. size");
  108.             System.out.println("4. check empty");
  109.             System.out.println("5. check full");
  110.             System.out.println("6. clear");
  111.  
  112.             int choice = scan.nextInt();
  113.             switch (choice)
  114.             {
  115.             case 1 : 
  116.                 System.out.println("Enter character to insert");
  117.                 cb.insert( scan.next().charAt(0) );                                        
  118.                 break;                         
  119.             case 2 : 
  120.                 System.out.println("Removed Element = "+ cb.delete());
  121.                 break;                         
  122.             case 3 : 
  123.                 System.out.println("Size = "+ cb.getSize());
  124.                 break;                            
  125.             case 4 : 
  126.                 System.out.println("Empty status = "+ cb.isEmpty());
  127.                 break;                
  128.             case 5 : 
  129.                 System.out.println("Full status = "+ cb.isFull());
  130.                 break; 
  131.             case 6 : 
  132.                 System.out.println("\nBuffer Cleared\n");
  133.                 cb.clear();
  134.                 break;                                    
  135.             default : System.out.println("Wrong Entry \n ");
  136.                 break;
  137.             }
  138.             /* display Buffer */
  139.             cb.display();     
  140.  
  141.             System.out.println("\nDo you want to continue (Type y or n) \n");
  142.             ch = scan.next().charAt(0);
  143.  
  144.         } while (ch == 'Y'|| ch == 'y');                                                        
  145.     }    
  146. }

advertisement
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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.