Monday, June 30, 2025

🚢‍♂️🚢‍♂️🚢‍♂️ DSA using Python: QUEUE 🚢‍♀️🚢‍♀️🚢‍♀️


Queue

🧾 What is a Queue?

A Queue is a linear data structure that follows the FIFO (First In, First Out) rule.
This means the element that is inserted first will be removed first — just like a line of people waiting for a ticket.

🧠 Real-life Example:

Like standing in a queue at a bus stop — the person who comes first gets on the bus first.

✅ Basic Queue Operations

Operation

Meaning

enqueue()

Add (insert) an element to the rear (end) of the queue

dequeue()

Remove an element from the front of the queue

peek() or front()

Look at the front element without removing it

isEmpty()

Check if the queue has no elements

isFull() (for fixed-size queues)

Check if the queue is full




πŸ’‘ Queue Implementation in Python using List

πŸ”· 1. enqueue() – Add element to the end

queue = [] queue.append(10) queue.append(20) queue.append(30) print("Queue after enqueue:", queue)

πŸ“Œ Output:

Queue after enqueue: [10, 20, 30]

πŸ”· 2. dequeue() – Remove element from the front

if queue: removed = queue.pop(0) print("Removed element:", removed) print("Queue after dequeue:", queue) else: print("Queue is empty")

πŸ“Œ Output:

Removed element: 10 Queue after dequeue: [20, 30]




πŸ”· 3. peek() – View the front element

if queue: print("Front element is:", queue[0]) else: print("Queue is empty")

πŸ“Œ Output:

Front element is: 20



πŸ”· 4. isEmpty() – Check if queue is empty

if not queue: print("Queue is empty") else: print("Queue is not empty")

πŸ“Œ Output:

Queue is not empty

πŸ”· 5. isFull() – (Used only when queue size is fixed)

Let’s say we fix size to 3:

MAX_SIZE = 3 if len(queue) == MAX_SIZE: print("Queue is full") else: print("Queue is not full")

πŸ“Œ Output (if queue = [20, 30]):

Queue is not full

🧠 Summary:

  • Queue = first in, first out (FIFO)

  • Main operations: enqueue, dequeue, peek, isEmpty, isFull

  • You can use list, or for efficiency, use collections.deque in Python





Types of Queue

πŸ”’ Types of Queues (with Syntax & Examples)

Queues are not limited to just one form. There are 5 main types of queues:


Simple Queue


✅ 1. Simple Queue (or Linear Queue)

πŸ“˜ Definition:

A simple queue allows insertion at the rear and deletion from the front. It follows the FIFO rule.

πŸ§‘‍πŸ’» Syntax & Example in Python:

queue = [] queue.append(10) # Enqueue queue.append(20) print("Queue:", queue) queue.pop(0) # Dequeue print("After Dequeue:", queue)

πŸ“Œ Output:

Queue: [10, 20] After Dequeue: [20]


Circular Queue


πŸ” 2. Circular Queue

πŸ“˜ Definition:

In a circular queue, the last position is connected back to the first to make a circle.
It helps in efficient use of memory when the queue is full but has empty space due to dequeued elements.

πŸ§‘‍πŸ’» Syntax & Example using List (basic logic):

size = 5 queue = [None] * size front = rear = -1 # Enqueue example rear = (rear + 1) % size queue[rear] = 10 # Dequeue example front = (front + 1) % size queue[front] = None

✔️ Real implementation is done with modulus operator % to loop around the queue.


Deque

πŸ”‚ 3. Deque (Double-Ended Queue)

πŸ“˜ Definition:

A Deque allows insertion and deletion from both front and rear.
There are two types:

  • Input Restricted Deque (insert at one end only)

  • Output Restricted Deque (delete at one end only)

πŸ§‘‍πŸ’» Syntax & Example using collections.deque:

from collections import deque dq = deque() dq.append(10) # Add to rear dq.appendleft(20) # Add to front print("Deque:", dq) dq.pop() # Remove from rear dq.popleft() # Remove from front print("After operations:", dq)

πŸ“Œ Output:

Deque: deque([20, 10]) After operations: deque([])


Priority Queue


πŸ›‘ 4. Priority Queue

πŸ“˜ Definition:

In a priority queue, each element has a priority.
Elements with higher priority are dequeued first, even if they were added later.

πŸ§‘‍πŸ’» Syntax & Example using queue.PriorityQueue:

from queue import PriorityQueue pq = PriorityQueue() pq.put((1, "Low Priority")) pq.put((0, "High Priority")) # 0 = highest priority while not pq.empty(): print(pq.get())

πŸ“Œ Output:

(0, 'High Priority') (1, 'Low Priority')


Double Ended Priority Queue

πŸ”’ 5. Doubly Ended Priority Queue (DEPQ)

πŸ“˜ Definition:

A DEPQ allows insertion and deletion at both ends with priorities assigned to elements.
(Not commonly used in basic programming; used in advanced cases like AI, scheduling, etc.)

🧠 Summary Table



No comments:

Post a Comment

➡️DSA using Python: Assignment-8: Stack Extending List

This assignment is designed for practicing Stack implementation using Python by extending the built-in list class. It helps you learn how ...