Queue
Queue
Tugume Brenda
QUEUE
Similar to stacks, a queue is also an Abstract Data Type or ADT. A queue
follows FIFO (First-in, First out) policy.
For example, a new person enters a queue at the last and the person who is
at the front (who must have entered the queue at first) will be served first.
Similar to a queue of day to day life, in Computer Science also, a new element
enters a queue at the last (tail of the queue) and removal of an element occurs
from the front (head of the queue)
Similar to the stack, we will implement the queue using a linked list as well as
with an array. But let’s first discuss the operations which are done on a queue.
Front → It is similar to the top operation of a stack i.e., it returns the front
element of the queue (but don’t delete it).
Queue is used to implement many algorithms like Breadth First Search (BFS),
etc.
• It can be also used by an operating system when it has to schedule jobs with
equal priority
• Customers calling a call center are kept in queues when they wait for
someone to pick up the calls
Queue Using an Array
We will maintain two pointers - tail and head to represent a queue. head will
always point to the oldest element which was added and tail will point where
the new element is going to be added.
To insert any element, we add that element at tail and increase the tail by one
to point to the next element of the array.
Suppose tail is at the last element of the queue and there are empty blocks
before head as shown in the picture given below.
In this case, our tail will point to the first element of the array and will follow a
circular order.
Initially, the queue will be empty i.e., both head and tail will point to the same
location i.e., at index 1. We can easily check if a queue is empty or not by
checking if head and tail are pointing to the same location or not at any time.
Similarly, we will say that if the head of a queue is 1 more than the tail, the
queue is full.
Queue Overflow
Now, we have to deal with the enqueue and the dequeue operations.
To enqueue any item to the queue, we will first check if the queue is full or not
i.e.,
If the queue is not full, we will add the element to the tail i.e, Q[Q.tail] = x.
While adding the element, it might be possible that we have added the
element at the last of the array and in this case, the tail will go to the first
element of the array.
Otherwise, we will just increase the tail by 1.
To dequeue, we will first check if the queue is empty or not. If the queue is
empty, then we will throw an error.
To dequeue, we will first store the item which we are going to delete from the
queue in a variable because we will be returning it at last.
Now, we just have to increase the head pointer by 1. And in the case when the
head is at the last element of the array, it will go 1.
Python Queue
Queue Using Linked List
As we know that a linked list is a dynamic data structure and we can change
the size of it whenever it is needed.
So, we are not going to consider that there is a maximum size of the queue
and thus the queue will never overflow.
However, one can set a maximum size to restrict the linked list from growing
more than that size.
As told earlier, we are going to maintain a head and a tail pointer to the
queue. In the case of an empty queue, head will point to NULL.
We will point the head pointer to the first element of the linked list and the tail
pointer to the last element of it as shown in the picture given below.
The enqueue operation simply adds a new element to the last of a linked list.
However, if the queue is empty, we will simply make the new node head and
tail of the queue.
To dequeue, we need to remove the head of the linked list.
To do so, we will first store its data in a variable because we will return it at
last and then point head to its next element.
We will execute the above codes when the queue is not empty. If it is, we will
throw the "Queue Underflow" error.
Queue using Linked List
Priority Queues
A queue has FIFO (first-in-first-out) ordering where items are taken out or
accessed on a first-come-first-served basis.
2. When we remove a data from a priority queue(min), the data at the top,
which will be the data with least priority, will get removed.
3. But, this way priority queue will not be following the basic priniciple of a
queue, First in First Out(FIFO).
o If the new node has the highest priority, then we will add the new node at
the end.
4. size: To check the size of the priority queue, in other words count the
number of elements in the queue and return it.