Python Tutorials · Python DSA

Queues

Learn all about Queues in this comprehensive tutorial.

5 min read intermediate
  • A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle.
  • Think of a queue as people standing in line in a supermarket.
  • For Python lists (and arrays), a Queue can look and behave like this:
  • Here's a complete implementation of a Queue class:
  • A linked list consists of nodes with some sort of data, and a pointer to the next node.
  • Queues are used in many real-world scenarios:

Introduction

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle.

Queues

Think of a queue as people standing in line in a supermarket.

The first person to stand in line is also the first who can pay and leave the supermarket.

Basic operations we can do on a queue are:

  • **Enqueue: **Adds a new element to the queue.
  • **Dequeue: **Removes and returns the first (front) element from the queue.
  • **Peek: **Returns the first element in the queue.
  • **isEmpty: **Checks if the queue is empty.
  • **Size: **Finds the number of elements in the queue.

Queues can be implemented by using arrays or linked lists.

Queues can be used to implement job scheduling for an office printer, order processing for e-tickets, or to create algorithms for breadth-first search in graphs.

Queues are often mentioned together with Stacks, which is a similar data structure described on the previous page.

Queue Implementation using Python Lists

For Python lists (and arrays), a Queue can look and behave like this:

Since Python lists has good support for functionality needed to implement queues, we start with creating a queue and do queue operations with just a few lines:

python
Note: Note: While using a list is simple, removing elements from the beginning (dequeue operation) requires shifting all remaining elements, making it less efficient for large queues.

Implementing a Queue Class

Here's a complete implementation of a Queue class:

python

Queue Implementation using Linked Lists

A linked list consists of nodes with some sort of data, and a pointer to the next node.

A singly linked list.

A big benefit with using linked lists is that nodes are stored wherever there is free space in memory, the nodes do not have to be stored contiguously right after each other like elements are stored in arrays. Another nice thing with linked lists is that when adding or removing nodes, the rest of the nodes in the list do not have to be shifted.

To better understand the benefits with using arrays or linked lists to implement queues, you should check out this page that explains how arrays and linked lists are stored in memory.

This is how a queue can be implemented using a linked list.

python

Reasons for using linked lists to implement queues:

  • **Dynamic size: **The queue can grow and shrink dynamically, unlike with arrays.
  • **No shifting: **The front element of the queue can be removed (enqueue) without having to shift other elements in the memory.

Reasons for not using linked lists to implement queues:

  • **Extra memory: **Each queue element must contain the address to the next element (the next linked list node).
  • **Readability: **The code might be harder to read and write for some because it is longer and more complex.

Common Queue Applications

Queues are used in many real-world scenarios:

  • Task scheduling in operating systems
  • Breadth-first search in graphs
  • Message queues in distributed systems

Module quiz

2 questions
1

Which of the following is true about Queues?

2

What is the most common pitfall when working with Queues?

Answer all questions to submit.