Module 8 QUEUES
Module 8 QUEUES
QUEUES
Joie Ann M. Mac
Objectives:
• To learn about Queues
• Operations
• Implementations
WHAT IS A QUEUE?
• A queue is a linear data structure that stores items in First In
First Out (FIFO) manner. With a queue the least recently
added item is removed first. A good example of queue is any
queue of consumers for a resource where the consumer that
came first is served first.
WHAT IS A QUEUE?
What are the Methods Available for
Queue in Python?
There are numerous methods available in Python to perform
operations on the queue. Some of the standard methods are:
• put(item): Inserts an element to the queue
• get(): Gets an element from the queue
• empty(): Checks and returns true if the queue is empty
• qsize: Returns queue’s length
• full(): Checks and returns true if the queue is full
• maxsize(): Maximum elements allowed in a queue
Basic Operations of Queue
A queue is an object (an abstract data structure - ADT) that
allows the following operations:
• Enqueue: Add an element to the end of the queue
• Dequeue: Remove an element from the front of the queue
• IsEmpty: Check if the queue is empty
• IsFull: Check if the queue is full
• Peek: Get the value of the front of the queue without
removing it
Working of Queue
Queue operations work as follows:
• two pointers FRONT and REAR
• FRONT track the first element of the queue
• REAR track the last element of the queue
• initially, set value of FRONT and REAR to -1
Enqueue Operation
• check if the queue is full
• for the first element, set the value of FRONT to 0
• increase the REAR index by 1
• add the new element in the position pointed to by REAR
Dequeue Operation
• check if the queue is empty
• return the value pointed by FRONT
• increase the FRONT index by 1
• for the last element, reset the values of FRONT and REAR to
-1
Types of Queues
A queue is a useful data structure in programming. It is similar to
the ticket queue outside a cinema hall, where the first person
entering the queue is the first person who gets the ticket.
There are four different types of queues:
• Simple Queue
• Circular Queue
• Priority Queue
• Double Ended Queue
Simple Queue
• In a simple queue, insertion takes place at the rear and
removal occurs at the front. It strictly follows the FIFO (First
in First out) rule.
Circular Queue
• In a circular queue, the last element points to the first
element making a circular link.
Priority Queue
• A priority queue is a special
type of queue in which
each element is associated
with a priority and is
served according to its
priority. If elements with
the same priority occur,
they are served according
to their order in the queue.
Double Ended Queue
• In a double ended queue, insertion and removal of elements
can be performed from either from the front or rear. Thus, it
does not follow the FIFO (First In First Out) rule.