One Pager Cheat Sheet
- A queue is a collection of items whereby its operations are arranged in a
FIFO
(First In First Out) format and involve two main operations:enqueue
anddequeue
. - The queue is a data structure with two open ends, the rear for adding elements and the front for removing them, which follows the
First In First Out (FIFO)
principle. - Queues work on the First In First Out (FIFO) principle of
enqueuing
anddequeuing
elements. - A Queue has two main operations - Enqueue to add elements and Dequeue to remove elements.
- The
enqueue
operation adds elements to the rear end of thequeue
, and updates thefront
andrear
pointer accordingly. - To
dequeue
an element from a queue, it is removed from the front end, if it is present, and at least one element must be in the queue for it to be possible. - Dequeuing an element from the queue does not affect the
front
pointer, which remains the same, pointing at the first element added, while therear
pointer increases with new elements. - We can implement queues using a
list
,collections.deque
, orqueue.Queue
inpython
, with thelist
involving costlyO(n)
time complexity but with the use of the round-robin technique also requiringO(1)
time, or usingcollections.deque
orqueue.Queue
requiringO(1)
time complexity. - The complexity of implementing a queue with a
list
class in any programming language is O(n) due to the time needed to shift all items on each addition or deletion. - We can quickly
enqueue
anddequeue
items in a queue using most languages by either using alist
orarray
, or with more efficient time complexity by implementing a fixed-sized list with two pointers. - The
Linked List
implementation ofQueues
is similar to the Linked List implementation ofStacks
we used previously. - The python library provides the
queue
class from theQueue
module to enable users to implement simple queues and check for the empty and full conditions using theempty()
andfull()
functions respectively. - We can implement a
queue
using thedeque
class from the collections module toappend()
andpopleft()
elements from the queue. - The
deque
class from thecollections
module can be used topopleft()
the first element and reduce the size of the queue in Python. - The FIFO approach and using
collections
are effective approaches forimplementing queues
in python.