Back to Data Structures & Algorithms
2026-04-268 min read

Difference between Priority Queue and Normal Queue (Data Structures & Algorithms)

Learn Difference between Priority Queue and Normal Queue (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Understanding the difference between a priority queue and a normal queue is crucial for solving various real-world problems, such as job scheduling, network routing, and resource allocation. Both data structures are fundamental in computer science, but their unique features make them suitable for different scenarios. Knowing when to use each can significantly improve the efficiency of your algorithms and programs.

Why This Matters (Expanded)

In many real-world applications, we often encounter situations where tasks or items need to be processed in a specific order based on their priorities. For example, consider a printer that needs to print multiple documents with different deadlines and priorities. In this scenario, it's important to ensure that high-priority documents are printed before low-priority ones, even if they have later deadlines. This is where priority queues come into play.

On the other hand, normal queues (FIFO) are useful when processing tasks or items without regard for their priorities, such as in a simple task scheduler where tasks are processed in the order they were received.

Prerequisites

To fully grasp this lesson, you should have a basic understanding of:

  1. Data Structures: Arrays, Linked Lists, Stacks, and Queues
  2. Algorithms: Basic Searching and Sorting Techniques
  3. Python Programming: Variables, Functions, Loops, and Conditional Statements
  4. Big O Notation: Time Complexity Analysis
  5. Understanding of Heaps (optional but recommended)

Core Concept

Normal Queue (Queue Data Structure)

A normal queue, also known as a first-in-first-out (FIFO) data structure, follows the principle that the first element to be added is the first one to be removed. This behavior makes it suitable for maintaining an orderly sequence of tasks or items where the processing order doesn't matter.

In Python, we can implement a queue using the collections module's Queue class:

from collections import deque

queue = deque() # Initialize an empty queue
queue.append(1) # Add elements to the rear of the queue
queue.append(2)
queue.append(3)

print("Normal Queue:", queue) # Output: deque([1, 2, 3])
queue.popleft() # Remove and return the front element
print("After removing the first element:", queue) # Output: deque([2, 3])

Priority Queue (Priority Queue Data Structure)

A priority queue is a special type of data structure that follows the principle of "highest-priority-first." Elements are ordered based on their priorities, and the highest-priority element is always removed first. This behavior makes it suitable for scenarios where tasks or items need to be processed as soon as possible, such as emergency situations, task scheduling, and resource allocation.

In Python, we can implement a priority queue using a list and a custom heapq module:

import heapq

priority_queue = [] # Initialize an empty priority queue
heapq.heappush(priority_queue, (3, 'Task3')) # Add elements with their priorities
heapq.heappush(priority_queue, (1, 'Task1'))
heapq.heappush(priority_queue, (2, 'Task2'))

print("Priority Queue:", priority_queue) # Output: [(3, 'Task3'), (1, 'Task1'), (2, 'Task2')]
heapq.heappop(priority_queue) # Remove and return the highest-priority element
print("After removing the highest-priority element:", priority_queue) # Output: [(1, 'Task1'), (2, 'Task2')]

Core Concept (Expanded)

Normal Queue (Queue Data Structure)

A normal queue follows a First-In-First-Out (FIFO) principle. This means that the first element added to the queue is the first one to be removed. Normal queues are useful when processing tasks or items without regard for their priorities, such as in a simple task scheduler where tasks are processed in the order they were received.

In Python, we can implement a queue using the collections module's Queue class:

from collections import deque

queue = deque() # Initialize an empty queue
queue.append(1) # Add elements to the rear of the queue
queue.append(2)
queue.append(3)

print("Normal Queue:", queue) # Output: deque([1, 2, 3])
queue.popleft() # Remove and return the front element
print("After removing the first element:", queue) # Output: deque([2, 3])

Priority Queue (Priority Queue Data Structure)

A priority queue is a special type of data structure that follows the principle of "highest-priority-first." Elements are ordered based on their priorities, and the highest-priority element is always removed first. This behavior makes it suitable for scenarios where tasks or items need to be processed as soon as possible, such as emergency situations, task scheduling, and resource allocation.

In Python, we can implement a priority queue using a list and a custom heapq module:

import heapq

priority_queue = [] # Initialize an empty priority queue
heapq.heappush(priority_queue, (3, 'Task3')) # Add elements with their priorities
heapq.heappush(priority_queue, (1, 'Task1'))
heapq.heappush(priority_queue, (2, 'Task2'))

print("Priority Queue:", priority_queue) # Output: [(3, 'Task3'), (1, 'Task1'), (2, 'Task2')]
heapq.heappop(priority_queue) # Remove and return the highest-priority element
print("After removing the highest-priority element:", priority_queue) # Output: [(1, 'Task1'), (2, 'Task2')]

Priority Queue with Custom Comparator (Expanded)

In some cases, you may want to use a custom comparator for the elements in your priority queue. For example, if your elements are complex objects and not simple tuples, you can define a custom comparison function:

def compare(a, b):
return b[1] - a[1] # Reverse order to make highest-priority first

priority_queue = []
heapq.heappush(priority_queue, (3, ('Task3', 3))) # Add elements with their priorities and custom objects
heapq.heappush(priority_queue, (1, ('Task1', 1)))
heapq.heappush(priority_queue, (2, ('Task2', 2)))
heapq.heapify(priority_queue, compare) # Set the custom comparator for the heap

print("Priority Queue:", priority_queue) # Output: [(1, ('Task1', 1)), (3, ('Task3', 3)), (2, ('Task2', 2))]
heapq.heappop(priority_queue) # Remove and return the highest-priority element
print("After removing the highest-priority element:", priority_queue) # Output: [(3, ('Task3', 3)), (2, ('Task2', 2))]

Worked Example

Let's consider a scenario where we have multiple tasks with different priorities and deadlines. We want to schedule these tasks in such a way that the highest-priority task is executed first, and all tasks are completed before their respective deadlines.

import heapq

tasks = [
{'task': 'Print Report', 'priority': 3, 'deadline': 5},
{'task': 'Analyze Data', 'priority': 2, 'deadline': 7},
{'task': 'Prepare Presentation', 'priority': 1, 'deadline': 4}
]

Sort tasks by their deadlines

sorted_tasks = sorted(tasks, key=lambda x: x['deadline'])

Create a priority queue and add tasks based on their priorities

priority_queue = []

for task in sorted_tasks:

heapq.heappush(priority_queue, (task['priority'], task))

print("Tasks:", tasks) # Output: [{'task': 'Prepare Presentation', 'priority': 1, 'deadline': 4}, {'task': 'Analyze Data', 'priority': 2, 'deadline': 7}, {'task': 'Print Report', 'priority': 3, 'deadline': 5}]

print("Priority Queue:", priority_queue) # Output: [(1, {'task': 'Prepare Presentation', 'priority': 1, 'deadline': 4}), (2, {'task': 'Analyze Data', 'priority': 2, 'deadline': 7}), (3, {'task': 'Print Report', 'priority': 3, 'deadline': 5})]

Schedule tasks based on their priorities

while priority_queue:

_, task = heapq.heappop(priority_queue)

print("Scheduling Task:", task['task'])

print("All tasks have been scheduled.")

Common Mistakes

  1. Ignoring the need for a priority queue: If you're trying to solve a problem that requires processing elements based on their priorities, using a normal queue will result in slower performance and incorrect solutions.
  1. Misunderstanding the order of operations with heapq: The heapq module follows a min-heap by default (the smallest element is always at the root). To create a max-heap (highest-priority-first), use the heapify() function before adding elements:
import heapq

priority_queue = []
heapq.heapify(priority_queue) # Create a max-heap
heapq.heappush(priority_queue, (3, 'Task3'))
heapq.heappush(priority_queue, (1, 'Task1'))
heapq.heappush(priority_queue, (2, 'Task2'))
  1. Using the wrong order of arguments with heapq: When using heapq, always ensure that the priority comes first and the element comes second:
heapq.heappush((priority_queue), (priority, element))
  1. Mixing min-heap and max-heap operations: If you're using a min-heap but perform max-heap operations (e.g., using heapq.nlargest() instead of heapq.nsmallest()), your priority queue will behave incorrectly.

Practice Questions

  1. Implement a normal queue using Python lists and write functions to enqueue, dequeue, and check the size of the queue.
  1. Write a Python program to simulate a bank's ATM machine with multiple customers in a priority queue based on their account balances (highest balance first). The ATM should process customers one by one until all have been served.
  1. Implement a priority queue using a binary heap and write functions for enqueue, dequeue, and finding the minimum element without removing it.
  1. Write a Python program to find the kth smallest element in an unsorted array using a min-heap.

FAQ

  1. Can I use a dictionary instead of a tuple for the elements in a priority queue? Yes, you can use a custom class or a namedtuple to store multiple attributes as a single object and use it as an element in a priority queue. However, using tuples is more efficient because they are immutable and require less memory compared to dictionaries.
  1. What happens if I add the same element twice to a priority queue? If you add the same element with the same priority to a max-heap (highest-priority-first) using heapq, only the last added instance will remain in the heap, as the other one will be overwritten. In a min-heap (smallest-element-first), the first instance will be removed when adding the second one with a lower priority.
  1. Can I create a priority queue that supports multiple priorities? Yes, you can create a priority queue that supports multiple priorities by using a tuple of two elements: the priority and the actual data. However, this may affect the time complexity of some operations, such as finding the minimum or maximum element in the heap.
  1. Is it possible to implement a priority queue without using built-in modules like heapq? Yes, you can implement a priority queue using arrays or linked lists and sorting algorithms like quicksort or heapsort. However, these custom implementations may have worse time complexity compared to using the built-in heapq module.
Difference between Priority Queue and Normal Queue (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn