Queues
Queues
Queues follow
the FIFO (First In First Out) principle, where the first element that’s added to the queue is the first
one to be removed. Queues are used in many algorithms and applications because of their simplicity
and efficiency in managing the data flow.
A queue is a linear data structure that is open at both ends, because of this, additions to the are made
at one end of the queue, and deletions are made at the other end of the queue. The first entry is
sometimes referred to as the head, and the last entry is sometimes referred to as the tail.
The difference between stacks and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the least recently added
➢ Input Restricted Queue – In this type, we can only input to end of the queue, but we can
delete from any end of the queue
➢ Output Restricted Queue – In this type, we can input to any end of the queue, but we can
only output from one end
➢ Circular queue: In this type, the last element is connected to the first element
➢ Double-ended queue – In this type, both, insertion and deletion can be performed from both
ends
➢ isFull – Returns true or false depending on whether the queue is full or not
➢ isNull – Returns true or false depending on whether the queue is empty or not
Implementation of Queues
Just like Stacks, Queues can also be implemented by arrays and linked lists
Array Implementation of Queues – In order to implement queues, we need to keep two indexes in
mind, the front index and the rear index, the indexes are incremented in a circular manner
To do this, we need to first check if the queue is full or not, if its full its considered an overflow and
we cannot proceed any further so we exit the program. If it isn’t empty we return the element at the
head and then increment the head
➢ Easy to implement
➢ Large amounts of data can managed efficiently
➢ Insertion and deletion operations are performed with ease because of the FIFO principle
➢ Dynamic size
➢ Ease of insertion and deletion operations
➢ Efficient memory utilisation – linked lists only assign memory to elements they contain
➢ Memory overhead – nodes require extra memory in order to store the pointers, this results in
higher memory usage
➢ No random access
➢ Slower operations – the enqueue and dequeue operations are slower in linked lists because
we need to dynamically allocate memory
Advantages of Queues
Disadvantages of Queues