0% found this document useful (0 votes)
14 views

Queue

The document discusses queues, which are an abstract data type that follows the FIFO (first-in, first-out) principle. A queue has operations like enqueue to add an element to the back and dequeue to remove an element from the front. Queues can be implemented using arrays or linked lists. The document also discusses priority queues, which order elements based on priority instead of just arrival order.

Uploaded by

New
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Queue

The document discusses queues, which are an abstract data type that follows the FIFO (first-in, first-out) principle. A queue has operations like enqueue to add an element to the back and dequeue to remove an element from the front. Queues can be implemented using arrays or linked lists. The document also discusses priority queues, which order elements based on priority instead of just arrival order.

Uploaded by

New
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

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.

It is equivalent to the queues in our general life.

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.

Enqueue → Enqueue is an operation which adds an element to the queue. As


stated earlier, any new item enters at the tail of the queue, so Enqueue adds
an item to the tail of a queue.
Dequeue → It is similar to the pop operation of stack i.e., it returns and
deletes the front element from the queue.
isEmpty → It is used to check whether the queue has any element or not.

isFull → It is used to check whether the queue is full or not.

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).

Before moving forward to code up these operations, let’s discuss the


applications of a queue.
Applications of Queue
Queues are used in a lot of applications, few of them are:

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.

Examples of queues include a queue at a movie ticket stand, as shown in the


illustration above. But, what is a priority queue?

A priority queue is an abstract data structure (a data structure defined by its


behaviour) that is like a normal queue but where each item has a special “key”
to quantify its “priority”.
For example, if the movie cinema decides to serve loyal customers first, it will
order them by their loyalty (points or number of tickets purchased).
In such a case, the queue for tickets will no longer be first-come-first-served,
but most-loyalfirst-served.
The customers will be the “items” of this priority queue while the “priority” or
“key” will be their loyalty.
A priority queue can be of two types:
1. Max Priority Queue: Which arranges the data as per descending order of
their priority.
2. Min Priority Queue: Which arranges the data as per ascending order of
their priority.
In a priority queue, following factors come into play:

1. In priority queue, data when inserted, is stored based on its priority.

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).

Yes, it won't! Because a priority queue is an advanced queue used when we


have to arrange and manipulate data as per the given priority.
Implementing Priority Queue
So now we will design our very own minimum priority queue using python list
and object oriented concept
Below are the algorithm steps:
1. Node: The Node class will be the element inserted in the priority queue. You
can modify the Node class as per your requirements.
2. insert: To add a new data element(Node) in the priority queue.
o If the priority queue is empty, we will insert the element to it.
o If the priority queue is not empty, we will traverse the queue, comparing
the priorities of the existing nodes with the new node, and we will add the
new node before the node with priority greater than the new node.

o If the new node has the highest priority, then we will add the new node at
the end.

3. delete: To remove the element with least priority.

4. size: To check the size of the priority queue, in other words count the
number of elements in the queue and return it.

5. show: To print all the priority queue elements.


Priority Queue Program

You might also like