0% found this document useful (0 votes)
11 views3 pages

Python Queue Implementation Guide

A Queue is a linear data structure that operates on a FIFO basis, allowing elements to be added and removed in the order they were inserted. It can be implemented using Python lists or a class, with basic operations including enqueue, dequeue, peek, is_empty, and size. Queues are commonly used in applications such as task scheduling and print queue management.

Uploaded by

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

Python Queue Implementation Guide

A Queue is a linear data structure that operates on a FIFO basis, allowing elements to be added and removed in the order they were inserted. It can be implemented using Python lists or a class, with basic operations including enqueue, dequeue, peek, is_empty, and size. Queues are commonly used in applications such as task scheduling and print queue management.

Uploaded by

mpicture620
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Queue Implementation in Python

A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. That
means the first element added is the first one removed, just like a real-life queue at a bank
or supermarket.

Basic Queue Operations


Operation Description

enqueue(item) Add an item to the rear of the queue

dequeue() Remove and return the front item

peek() or front() View the item at the front without


removing it

is_empty() Check if the queue is empty

size() Get the number of elements in the queue

1. Queue Using Python List


Python lists can be used to create a simple queue using append() and pop(0).

# Queue implementation using list

queue = []

# Enqueue elements
[Link](10)
[Link](20)
[Link](30)
print("Queue after enqueues:", queue)

# Dequeue element
front = [Link](0)
print("Dequeued element:", front)
print("Queue after dequeue:", queue)

# Peek front element


print("Front element:", queue[0])

# Check if queue is empty


print("Is queue empty?", len(queue) == 0)
Output:
Queue after enqueues: [10, 20, 30]
Dequeued element: 10
Queue after dequeue: [20, 30]
Front element: 20
Is queue empty? False

2. Queue Using a Class


Let's make a more structured and reusable version of a queue using a class.

class Queue:
def __init__(self):
[Link] = []

def enqueue(self, item):


[Link](item) # add to rear

def dequeue(self):
if not self.is_empty():
return [Link](0) # remove from front
return "Queue is empty"

def peek(self):
if not self.is_empty():
return [Link][0] # front element
return "Queue is empty"

def is_empty(self):
return len([Link]) == 0

def size(self):
return len([Link])

# Example usage
q = Queue()
[Link](5)
[Link](10)
[Link](15)
print("Queue:", [Link])
print("Front element:", [Link]())
print("Dequeued:", [Link]())
print("Queue after dequeue:", [Link])
Output:
Queue: [5, 10, 15]
Front element: 5
Dequeued: 5
Queue after dequeue: [10, 15]

3. Application Example – Task Scheduling


Queues are widely used in real-life applications such as task scheduling, print queue
management, and CPU job scheduling.

tasks = Queue()
[Link]("Task 1")
[Link]("Task 2")
[Link]("Task 3")

while not tasks.is_empty():


print("Processing:", [Link]())

Output:
Processing: Task 1
Processing: Task 2
Processing: Task 3

Summary
- Queue follows FIFO (First In First Out) principle
- Can be implemented using Python list or a class
- Common uses:
• Task scheduling
• Message queues
• Resource management
• Print spooling systems

You might also like