Course contentsShow
← Computer Science
Lesson 389 of 2,8729. Stacks and QueuesFree lesson

Priority Queues Introduction

Queues where elements are served based on priority rather than insertion order.

Priority Queues Introduction

What you'll learn: How priority queues differ from regular queues by serving elements based on importance rather than arrival order.

The Core Idea

In a regular queue, you follow the FIFO (First-In-First-Out) principle—whoever arrives first gets served first, like a line at a grocery store. But what if some items are more urgent than others?

A priority queue is a data structure where each element has an associated priority, and elements are dequeued in order of their priority, not their arrival time. The highest-priority element is always served first, regardless of when it was added.

Real-World Analogy

Think of a hospital emergency room. Patients don't get treated strictly in arrival order. Someone with a critical injury (high priority) gets immediate attention, even if they arrived after someone with a minor cut (low priority). The emergency room operates like a priority queue.

Key Differences from Regular Queues

  • Regular Queue: First in, first out
  • Priority Queue: Highest priority out first

When you insert an element into a priority queue, you also specify (or the element carries) its priority. When you remove an element, you always get the one with the highest priority among all elements currently in the queue.

Common Use Cases

Priority queues appear everywhere in computer science: task scheduling in operating systems, network packet routing, pathfinding algorithms, and event simulation systems—anywhere you need to process "important" items before "less important" ones.

Key Takeaway: Priority queues serve elements based on priority levels rather than insertion order, making them essential for any system where some tasks are more urgent than others.