Implementation
Let's use python
for our implementation. In python
, queues can be implemented using three different modules from the python library.
- list (using a
list
orarray
is generalizable to most languages) - collections.deque (language-specific)
- queue.Queue (language-specific)
Using the list
class can be a costly affair since it involves shifting of elements for every addition or deletion. This requires O(n)
time. But, with the round-robin technique, even with lists, we can achieve the time complexity of O(1)
. We have shown the implementation below.
Instead, we can also use the 'deque' class, which is a shorthand for 'Double-ended queue' and requires O(1)
time, which is much more efficient.