This question evaluates knowledge of data structure design, generic types, in-place memory allocation, and constant-time FIFO operations required for implementing a fixed-capacity circular buffer.
Design a data structure that stores generic elements T with the following properties:
pop()
returns the oldest element.
push()
and
pop()
must be
O(1)
.
push()
a new element, it
overwrites the oldest
element.
Implement a circular buffer class, e.g. CircularBuffer<T, N> where:
T
is the element type
N
is the fixed capacity (example:
N = 5
)
Implement at least:
push(value)
pop()
print()
(prints elements from oldest to newest)
pop()
is called on an empty buffer?