Veröffentlicht von & unter Javascript.

A ring array, also known as a circular array, is a data structure that behaves like an array but the elements are connected in a circular fashion. In other words, the last element of the array is followed by the first element, forming a loop.

Here’s an example of how you can implement a ring array in JavaScript:

class RingArray {
  constructor(size) {
    this.array = new Array(size);
    this.size = size;
    this.head = 0;
    this.tail = 0;

  push(element) {
    this.array[this.tail] = element;
    this.tail = (this.tail + 1) % this.size;

  pop() {
    if (this.head === this.tail) return undefined;
    const element = this.array[this.head];
    this.head = (this.head + 1) % this.size;
    return element;

To use this ring array, you can create an instance of it and then use the push method to add elements to the end of the array, and the pop method to remove elements from the beginning of the array. When the array is full and you try to push a new element, the oldest element will be overwritten.

Here’s an example of how you can use the ring array:

const ringArray = new RingArray(3);
console.log(ringArray.pop()); // prints 1
console.log(ringArray.pop()); // prints 2
console.log(ringArray.pop()); // prints 3
console.log(ringArray.pop()); // prints 4