Open In App

Java Program to Implement Circular Buffer

Last Updated : 31 Aug, 2021
Comments
Improve
Suggest changes
1 Like
Like
Report

When data is constantly moved from one place to another or from one process to another or is frequently accessed, it cannot be stored in permanent memory locations such as hard drives as they take time to retrieve the data. This type of data needs to be accessed quickly and is stored in temporary memory locations such as RAM known as buffers. 

Examples of Buffer:

  • When any video is streamed online, the data (the audio and video)is buffered right before the video is played. During this buffering process, the data is downloaded and stored in the RAM and is accessed whenever needed.
  • A Word document stores the content and the changes made by the user in a buffer before the document is saved.

What is a Circular Buffer?

Circular Buffer or Ring Buffer is a circular queue that allows the usage of memory in a contiguous manner. Circular Buffer follows the FIFO principle i.e First In First Out. 

Circular Buffers can be implemented in two ways, using an array or a linked list.

Approach 1: Using an Array

An empty object array along with its capacity is initialized inside the constructor as the type of elements added is unknown. Two pointers namely head and tail are maintained for insertion and deletion of elements. The head points to the first element and the tail points to the last element.

Circular Buffer using an array

Insertion of elements

Initially, the head is 0, the tail is -1 and the size is 0. 

The index at which the element needs to be inserted is calculated using the formula: -

int index = (tail + 1) % capacity
array[index] = element;

The tail pointer and the size increment by one upon insertion of an element. When the size of the array becomes equal to its capacity, the buffer is full and no more elements can be accommodated.

Deletion of elements:

The element at the head pointer is retrieved and the head pointer is incremented by one and the size of the buffer if decremented by one.

int index = head % capacity;
E element = (E) array[index];

Example:

Input : [5, 6, 7, 1 ,4]

Output : The elements are printed in the order :-
          5
          6
          7
          1
          4

Below is the implementation of the above approach


Output
The elements are printed in the order :-
5
6
7
1
4

Time complexity: O(1), for both insertion and deletion.

Approach 2: Using a Linked List

A Generic Node class is created which acts as a helper class to create the Circular Buffer.

Two pointers namely head and tail are maintained for insertion and deletion of elements. The head points to the first element and the tail points to the last element.

Circular Buffer using Linked List

Insertion of elements: 

  • Initially the head and tail are null and the size is 0.
  • Elements are added to the tail of the Linked List and the reference of the tail is changed to the head pointer.
  • Size of the Buffer increases as elements are added into the Linked List.
  • When the size of the array becomes equal to its capacity, the buffer is full and no more elements can be accommodated.

Deletion of elements:

The element at the head pointer is retrieved and the reference of the head pointer changes to the next element and the size of the buffer if decremented by one.

Example : 

Input : [5, 6, 7, 1 ,4]
Output: The elements are printed in the order :
          5
          6
          7
          1
          4

Below is the implementation of the above approach: 


Output
The elements are printed in the order :-
5
6
7
1
4

Time complexity: O(1), for both insertion and deletion.


 


Next Article

Similar Reads