![]() |
| 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
Operation | Meaning |
|---|---|
| Add (insert) an element to the rear (end) of the queue |
| Remove an element from the front of the queue |
| Look at the front element without removing it |
| Check if the queue has no elements |
| Check if the queue is full |
π· 1. enqueue() – Add element to the end
π Output:
π· 2. dequeue() – Remove element from the front
π Output:
π· 3. peek() – View the front element
π Output:
π· 4. isEmpty() – Check if queue is empty
π Output:
π· 5. isFull() – (Used only when queue size is fixed)
Let’s say we fix size to 3:
π Output (if queue = [20, 30]):
π§ Summary:
-
Queue = first in, first out (FIFO)
-
Main operations: enqueue, dequeue, peek, isEmpty, isFull
-
You can use
list, or for efficiency, usecollections.dequein 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:
π Output:
π 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):
✔️ 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:
π Output:
Deque: deque([20, 10])
After operations: deque([])π 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:
π 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.)











No comments:
Post a Comment