9

What is the difference between a Ring Buffer and a Circular Linked List?

What purpose does Ring Buffer serve that Circular Linked List cannot or vice versa?

user366312
  • 17,582
  • 55
  • 198
  • 392

1 Answers1

12

A ring buffer is a single contiguous block of memory which contains your items and, when you reach the end, you cycle back to the start:

+----------------------+
|                      |
+-->| a | b | c | d |--+

=== increasing memory ===>

A circular linked list, due to the linked list nature, does not have to be contiguous at all, so all the elements can be scattered in memory. It simply obeys the property that the elements for a loop:

+---------| d |<-----------------+
|                                |
+-->| a |------------->| b |--+  |
                              |  |
            +-----------------+  |
            |                    |
            +-->| c |------------+

=== increasing memory ===>

The circular linked list has the same advantage over a ring buffer that a linked list has over a fixed array. It can vary in size and you can insert and delete items without shuffling.

The disadvantages are similar as well, no O(1) array access and increased work if you're expanding or contracting the list.

A ring buffer would tend to be used when you know the maximum allowed entries in it, or don't mind limiting it. For example, if you have a communications protocol that can throttle the sending side when the buffer becomes full, giving the receiving side time to catch up.

A circular linked list example would be a process list in an operating system where you need to be able to add or delete processes but you don't care about the head of the list, only the current item.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • OK. That was from implementation point of view. What is the difference from usage point of view? – user366312 Aug 20 '15 at 05:03
  • @anonymous, that depends entirely on how you use it :-) If you're just putting at the tail and getting at the head, there should be no difference at all in usage. I'll add some more notes. – paxdiablo Aug 20 '15 at 05:07