Simple Queue or Linear Queue
Simple Queue or Linear Queue
one end (rear) and the deletion is done at the other end (front). It has a drawback of wasting
space when the front elements are deleted and the rear reaches the end of the array.
Circular Queue: This type of queue overcomes the drawback of simple queue by connecting the
rear and front ends of the array. It forms a circular shape and allows insertion and deletion at
any position. It utilizes the memory efficiently and avoids overflow condition.
Priority Queue: This type of queue assigns a priority to each element and arranges them
accordingly. The element with the highest priority is removed first, regardless of its order of
insertion. It is useful for applications like CPU scheduling, disk scheduling, etc.
Double Ended Queue (or Deque): This type of queue allows insertion and deletion at both ends
(front and rear). It is more flexible than simple queue and can be used to implement stacks and
queues
A queue is a linear data structure that follows the First In First Out (FIFO) principle,
meaning that the element that is inserted first is removed first. There are two basic
ways to implement a queue: using an array or using a linked list.
Using a linked list: A linked list is a dynamic collection of nodes, where each node
contains some data and a pointer to the next node. To implement a queue using a
linked list, we need to maintain two pointers: front and rear. The front pointer points
to the first node of the queue, and the rear pointer points to the last node of the queue.
To enqueue an element, we create a new node with the element as data and make it
the next node of the rear node. We also update the rear pointer to point to the new
node.
To dequeue an element, we return the data of the front node and make the next node
of the front node as the new front node. We also update the front pointer to point to
the new front node. We also need to check if the queue is empty before performing
these operations. A possible advantage of using a linked list is that it can grow or
shrink according to the queue size, and it does not have a fixed capacity limit.
| 10 | 20 | 30 | 40 | 50 | | | |
^ ^
front rear
Enqueue 60:
| 10 | 20 | 30 | 40 | 50 | 60 | | |
^ ^
front rear
Dequeue:
| | 20 | 30 | 40 | 50 | 60 | | |
^ ^
front rear
Enqueue 60:
Dequeue: