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

Queues in Python

The document provides an overview of queues in Python, explaining their definition, types, and real-life examples. It covers four main types of queues: Simple/Linear Queue, Circular Queue, Priority Queue, and Double Ended Queue, detailing their operations and use cases. Additionally, it illustrates the FIFO principle and how queues can model various real-world scenarios.

Uploaded by

Metilda Mervina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Queues in Python

The document provides an overview of queues in Python, explaining their definition, types, and real-life examples. It covers four main types of queues: Simple/Linear Queue, Circular Queue, Priority Queue, and Double Ended Queue, detailing their operations and use cases. Additionally, it illustrates the FIFO principle and how queues can model various real-world scenarios.

Uploaded by

Metilda Mervina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

F

O
O
D

T
E
C

Queues in
H

Python
Understanding and Implementing Queues
01
Basics on Queue
What is a Queue?
A queue is a data structure that follows
the First-In-First-Out (FIFO) principle,
where elements are added at the back
and removed from the front. It models
real-world scenarios like waiting lines
Queues
❑ Linear data structure which stores its element in an ordered
manner
❑ It has two pointers , ‘Front’ that points to the beginning of
the queue and ‘Rear’ that points to the end of the queue
❑ A queue works on a FIFO [First-in-First-Out] principle
➢ The first element added to the queue is the first one to be
removed
❑ The element in a queue are added at one end and removed
from the other end
❑ In Computer system the OS makes fuel use Queues
Real-Life Examples of Queues
❑ People moving on an escalator
❑ People waiting for a bus
❑ People standing outside the ticketing window of
cinema hall
❑ Luggage kept on conveyor belts
❑ Cars lined at a roll bridge
02
Types of Queues
1.Simple/Linear Queue
❑ A linear queue is a type of queue where data elements
are added to the end of the queue and removed from the
front of the queue
❑ Linear queues are used in applications where data
elements need to be processed in the order in which they
are received
❑ Examples:
➢ Ticket Counter: The first person in line gets served first
➢ Printer Queue: Jobs are printed in the order they were submitted
1.Simple/Linear Queue
❑ Operations:
➢ Enqueue: Adding an element to the rear of the queue
➢ Dequeue: Removing an element from the front of the
queue
Code:
q = []
q.append('apple’)
q.append('banana’)
print(q.pop(0)) # apple goes out

Explanation:

•Think of a line at a shop.


•q = [ ] → Start with an empty line.
•append('apple') → Apple joins the line.
•append('banana') → Banana comes behind apple.
•pop(0) → The first one (apple) leaves first.
First In, First Out — just like waiting in line!
2. Circular Queue
❑ A circular queue is similar to a linear queue, but the end
of the queue is connected to the front of the queue
❑ This allows for efficient use of space in memory and can
improve performance
❑ Circular queues are used in applications where the data
elements need to be processed in a circular fashion
❑ Examples include CPU scheduling and memory
management
2. Circular Queue
❑ Operations:
➢ Enqueue: Adding an element to the rear of the queue
➢ Dequeue: Removing an element from the front of the
queue
➢ Is Full: Checks if the queue is full
➢ Is Empty: Checks if the queue is empty
3. Priority Queue
❑ A priority queue is a type of queue where each element is
assigned a priority level
❑ Elements with higher priority levels are processed before
elements with lower priority levels
❑ Priority queues are used in applications where certain
tasks or data elements need to be processed with higher
priority
❑ Examples include operating system task scheduling and
network packet scheduling
3. Priority Queue
❑ Operations:
➢ Insertion : If the newly inserted item is of the highest priority,
then it is inserted at the top. Otherwise, it is inserted in such a
way that it is accessible after all higher priority items are
accessed.
➢ Deletion : We typically remove the highest priority item which
is typically available at the top. Once we remove this item, we
need not move next priority item at the top.
➢ Peek : This operation only returns the highest priority item
(which is typically available at the top) and does not make any
change to the priority queue
4. Double Ended Queue
❑ A double-ended queue is a data structure that allows you
to add and remove items from both ends
❑ You can add items to either the front or the rear
❑ You can remove items from either the front or the rear
❑ These are different from stacks and queues because they
don't require the LIFO and FIFO orderings
Queue Type Idea Rule

Linear Queue Line of people First In, First Out

Round table
Circular Queue Goes in a circle
passing items

Priority Queue VIPs go first Small number first

Double Ended Two doors to


Both sides allowed
Queue enter/exit

You might also like