0% found this document useful (0 votes)
37 views24 pages

MBP 02 - Circular Buffer

Uploaded by

essenzed75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views24 pages

MBP 02 - Circular Buffer

Uploaded by

essenzed75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Implementation Ring

MBP-03 Circular Queue or Ring Buffer or Circulat Buffer


LIFO and FIFO
Stacks and Queues
 Stacks and queues are another popular data structures in C++. The
terms “Stacks and Queues” go hand in hand but they are two different
concepts which we will discuss briefly:
 In programming terminology, a stack is both an abstract data type as
well as a data structure that follows the LIFO rule (Last In First Out). It
means that the element inserted at the last is the first one to be
deleted.
In simple words, the insertion and deletion of data elements in a stack
can only be done from one end, that is, the top.
 A queue is also an abstract data type as well as a data structure but it
follows the FIFO rule (First In First Out). It means that the element
inserted first will be the first one to be deleted. In simple words,
insertion and deletion of data elements in a queue take place from two
ends – deletion from front and insertion from the rear.
Deque implementation

 A circular buffer or circular queue or cyclic buffer or ring buffer is single


and fixed size buffer with some programming logic to act as if the
elements are connected end-to-end.
 Circular buffer provides a buffering and queueing mechanism with the
help of a fixed size array often used in low memory footprint systems
and device drivers.
 Circular buffer can be implemented with static array or fixed size
dynamic array. It never calls any dynamic memory allocation routines
like new/delete/resize during runtime. It manages addition and deletion
of queue elements in a circular rotational fashion.
Class definition
Initialization

 Deque q = Deque();
Implement function
insertRear
Circular queue
Declaration
Advantages of circular buffer

 Simple logic using array and no complex data structures and logic used
 No dynamic allocation or deallocation and no chance of memory leak
and other issues
 Best suit for low memory footprint systems or for device drivers
 Very fast operations and do not include delay of new and delete
 These can be used in applications as well as low level codes in interrupt
handlers and real time systems
Limitations of circular buffer

 Queue is fixed in size,


and this cannot be changed.
 addition of new element will
overwrite old queue elements or
must
wait till some elements are free.
Circular buffer data structure

Circular buffer is a class and it has member variables to hold all the context of
a circular buffer. Circular buffer object should have these member variables
 Buff - The member buff points to an array which is a static array of fixed
size or dynamic array of fixed size.
 Start - Start will have the value of the starting index or offset of this
queue.
 End - End will have the value of the ending index or offset of this queue.
 Size - Size will have total number of array elements in the buffer. This
remains constant throughout the execution.
 Count - Count says the number of available elements in the queue. It
increases when an element gets added in the queue. It decreases once an
element dequeues from the array.
Empty
Is Empty
Insert item
insert the elements to
make a queue full

front == 0 && rear == size - 1


delete the two elements

i.e. item 1 and item 3

front = (front + 1) %
size
deQueue
insert or
enqueue
element 11
enQueue
Display

You might also like