Open In App

Queue Implementation in Python

Last Updated : 14 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Queue is a linear data structure that stores items in a First In First Out (FIFO) manner. With a queue, the least recently added item is removed first. One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginning of the line. It is an ordered list in which insertions are done at one end which is known as the rear and deletions are done from the other end known as the front. A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first.

Queue

Queues can be implemented in multiple ways, including:

  1. Using a List (Simple but inefficient for frequent deletions)
  2. Using a Circular List (More efficient than a simple list, but frequent resizing may cause inefficiency)
  3. Using a Linked List (Dynamic memory allocation with efficient insertions and deletions, always has O(1) time for insertion and deletion, but random access and cache friendliness missing)

Basic Operations on Queue

  • enqueue(): Inserts an element at the end of the queue i.e. at the rear end.
  • dequeue(): This operation removes and returns an element that is at the front end of the queue.
  • front(): This operation returns the element at the front end without removing it.
  • rear(): This operation returns the element at the rear end without removing it.
  • isEmpty(): This operation indicates whether the queue is empty or not.
  • isFull(): This operation indicates whether the queue is full or not.
  • size(): This operation returns the size of the queue i.e. the total number of elements it contains. 

1. Queue Implementation Using Linked List

A linked list-based queue dynamically allocates memory, making it efficient for frequent insertions and deletions. Unlike a list-based queue, it does not have shifting overhead.

Time Complexity:

OperationTime ComplexityExplanation
EnqueueO(1)Adds elements at the rear in constant time
DequeueO(1)Removes elements from the front efficiently
PeekO(1)Directly accesses the front element
GetRearO(1)Accesses the rear element instantly
Check if EmptyO(1)Uses front == None to check if empty
SizeO(1)Uses a size counter for quick lookup

Pros of Implementing Queue Using Linked List

  • Efficient enqueue and dequeue operations (O(1)), as no element shifting is needed.
  • Dynamically grows and shrinks without a predefined size, preventing memory waste.
  • Better for large queues with frequent insertions and deletions, as operations remain constant time.
  • No unnecessary memory usage after dequeuing, unlike a list-based queue.
  • Scalable for real-time applications, such as task scheduling and network packet management.

Challenges of Implementing Queue Using Linked List

  • Requires extra memory for storing next pointers in each node.
  • More complex to implement compared to a list-based queue.
  • Slower access to elements (O(n)) since there is no direct indexing like in lists.
  • Overhead of managing pointers, making it harder to debug and maintain.

To explore in detail refer - Implementation of Queue using Linked List in Python 

2. Queue Implementation Using List and Circular List

The simplest way to implement a queue in Python is by using a built-in list. However, removing elements from the front (pop(0)) has O(n) complexity, making it inefficient for large queues.

A circular queue is an optimized version that avoids shifting elements, making both enqueue and dequeue operations O(1). In this implementation, the last position wraps around to the first, ensuring efficient space utilization.

Time Complexity:

OperationRegular List QueueCircular QueueExplanation
EnqueueO(1)O(1)append() adds elements in constant time
DequeueO(n)O(1)pop(0) shifts all elements in a list, but in a circular queue, front just moves forward
PeekO(1)O(1)Accessing list[0] or arr[front] is constant time
Check if EmptyO(1)O(1)Uses len(list) == 0 or size == 0
SizeO(1)O(1)Uses len(list) or a size counter

Pros of Implementing Queue Using List & Circular List

  • Simple to implement using Python’s built-in list methods like append() and pop(0).
  • Dynamically resizable (for regular lists), avoiding the need to specify a fixed size.
  • Efficient enqueue operation (O(1)) since elements are added at the end using append().
  • Circular queue avoids shifting overhead, making both enqueue and dequeue O(1).
  • Suitable for small-scale applications where shifting cost is negligible.

Challenges of Implementing Queue Using List & Circular List

  • Regular lists are inefficient for dequeue operations (O(n)) because removing an element from the front requires shifting all elements.
  • Memory waste in regular lists occurs after multiple dequeues, leading to unused space.
  • Circular queues require additional logic to manage front and rear pointers properly.
  • Not ideal for large, dynamic queues due to performance degradation over time.

To explore in detail refer - Implementation of Queue using List in Python


Next Article
Article Tags :
Practice Tags :

Similar Reads