Ring - newell-romario/DSA GitHub Wiki

A ring is a circular buffer in the simplest terms. A ring uses First in First Out (FIFO) policy similar to a queue. The only difference between a queue and ring is that a ring overwrites the oldest element whenever it becomes full. Our ring implementation can be accessed by including the r2_ring.h.

Representation

Our ring is represented by the structure below:

struct r2_ring{
        void **data;/*array that stores data item*/
        r2_uint64 front; /*front of ring*/
        r2_uint64 rear;/*rear of ring*/
        r2_uint64 ncount;/*number of items in ring*/
        r2_uint64 rsize;/*size of the ring*/
        r2_cmp cmp;/*A comparison callback function*/
        r2_cpy cpy;/*A callback function to copy values*/
        r2_fd  fd;/*A callback function that release memory*/
}; 

The fields data, front, rear, ncount, and rsize should not be accessed or modified.

Creation

struct r2_ring *ring = r2_create_ring(size,cmp, cpy, fd);

Destruction

r2_destroy_ring(ring)

Insertion

int data = 1997;
r2_ring_insert(ring, data);

Deletion

r2_ring_delete(ring);

Front

//First element in the ring

int *data = r2_ring_front(ring); 
printf("\nFirst in line: %ld", *data);