September 11, 2024

Python Stack and Queue

Stacks and queues are fundamental data structures used in programming to store and manage collections of elements. In Python, these data structures can be implemented in various ways, often using lists or collections from the collections module. Understanding how to implement and use stacks and queues is essential for solving problems related to order, sequence, and priority.

1. Python Stack

A stack is a collection of elements that follows the Last In, First Out (LIFO) principle. The last element added to the stack is the first one to be removed.

1.1. Implementing a Stack Using a List

In Python, a list can be used to implement a stack by using the append() method to add elements to the top of the stack and the pop() method to remove elements from the top of the stack.

# Stack implementation using a list
stack = []

# Push elements onto the stack
stack.append(1)
stack.append(2)
stack.append(3)
print("Stack after pushing:", stack)

# Pop elements from the stack
top_element = stack.pop()
print("Popped element:", top_element)
print("Stack after popping:", stack)

The output will be:

Stack after pushing: [1, 2, 3]
Popped element: 3
Stack after popping: [1, 2]

1.2. Implementing a Stack Using collections.deque

Although lists can be used as stacks, the collections.deque class is preferred because it provides an efficient way to append and pop elements from both ends.

from collections import deque

# Stack implementation using deque
stack = deque()

# Push elements onto the stack
stack.append(1)
stack.append(2)
stack.append(3)
print("Stack after pushing:", stack)

# Pop elements from the stack
top_element = stack.pop()
print("Popped element:", top_element)
print("Stack after popping:", stack)

2. Python Queue

A queue is a collection of elements that follows the First In, First Out (FIFO) principle. The first element added to the queue is the first one to be removed.

2.1. Implementing a Queue Using collections.deque

The deque class from the collections module can also be used to implement a queue. The append() method adds elements to the end of the queue, and the popleft() method removes elements from the front of the queue.

from collections import deque

# Queue implementation using deque
queue = deque()

# Enqueue elements
queue.append(1)
queue.append(2)
queue.append(3)
print("Queue after enqueuing:", queue)

# Dequeue elements
front_element = queue.popleft()
print("Dequeued element:", front_element)
print("Queue after dequeuing:", queue)

The output will be:

Queue after enqueuing: deque([1, 2, 3])
Dequeued element: 1
Queue after dequeuing: deque([2, 3])

2.2. Implementing a Queue Using queue.Queue

The queue module provides a Queue class that is thread-safe and can be used to implement a FIFO queue.

import queue

# Queue implementation using queue.Queue
q = queue.Queue()

# Enqueue elements
q.put(1)
q.put(2)
q.put(3)
print("Queue size after enqueuing:", q.qsize())

# Dequeue elements
front_element = q.get()
print("Dequeued element:", front_element)
print("Queue size after dequeuing:", q.qsize())

The output will be:

Queue size after enqueuing: 3
Dequeued element: 1
Queue size after dequeuing: 2

3. Summary of Operations

Stack Operations

  • push(x): Add element x to the top of the stack.
  • pop(): Remove and return the element from the top of the stack.
  • peek(): Return the element at the top of the stack without removing it (implemented manually).
  • is_empty(): Check if the stack is empty (implemented manually).

Queue Operations

  • enqueue(x): Add element x to the end of the queue.
  • dequeue(): Remove and return the element from the front of the queue.
  • front(): Return the element at the front of the queue without removing it (implemented manually).
  • is_empty(): Check if the queue is empty (implemented manually).

Stacks and queues are essential data structures for managing collections of elements where the order of operations is important. In Python, they can be efficiently implemented using lists, collections.deque, or queue.Queue, depending on the specific requirements of your application.