Program to implement a queue that can push or pop from the front, middle, and back in Python

A flexible queue is a data structure that allows insertion and removal of elements from the front, middle, and back positions. This is useful when you need dynamic access to different parts of your queue.

Understanding the Queue Operations

Our queue supports six main operations ?

  • push_from_front() − Insert element at the beginning
  • push_from_middle() − Insert element at the middle position
  • push_from_back() − Insert element at the end
  • pop_from_front() − Remove and return first element
  • pop_from_middle() − Remove and return middle element
  • pop_from_back() − Remove and return last element

Implementation

Here's how to implement a flexible queue using Python's list ?

class FlexibleQueue:
    def __init__(self):
        self.queue = []

    def push_from_front(self, value):
        self.queue.insert(0, value)

    def push_from_middle(self, value):
        middle_index = len(self.queue) // 2
        self.queue.insert(middle_index, value)

    def push_from_back(self, value):
        self.queue.append(value)

    def pop_from_front(self):
        if self.queue:
            return self.queue.pop(0)
        return None

    def pop_from_middle(self):
        if self.queue:
            middle_index = (len(self.queue) - 1) // 2
            return self.queue.pop(middle_index)
        return None

    def pop_from_back(self):
        if self.queue:
            return self.queue.pop()
        return None

    def show_queue(self):
        return self.queue

# Example usage
queue = FlexibleQueue()
queue.push_from_back(10)
queue.push_from_back(20)
queue.push_from_front(30)
queue.push_from_middle(40)
queue.push_from_front(50)
print("After pushes:", queue.show_queue())

queue.pop_from_back()
print("After pop_back:", queue.show_queue())

queue.pop_from_front()
print("After pop_front:", queue.show_queue())

queue.pop_from_middle()
print("After pop_middle:", queue.show_queue())
After pushes: [50, 30, 40, 10, 20]
After pop_back: [50, 30, 40, 10]
After pop_front: [30, 40, 10]
After pop_middle: [30, 10]

How It Works

The middle position calculation uses integer division ?

  • For insertion: len(queue) // 2 gives the middle index
  • For removal: (len(queue) - 1) // 2 finds the middle element

Step-by-Step Execution

queue = FlexibleQueue()

# Building the queue
queue.push_from_back(10)    # [10]
queue.push_from_back(20)    # [10, 20]
queue.push_from_front(30)   # [30, 10, 20]
queue.push_from_middle(40)  # [30, 40, 10, 20] (inserted at index 1)
queue.push_from_front(50)   # [50, 30, 40, 10, 20]

print("Step by step:")
print("Initial queue:", queue.show_queue())
print("Length:", len(queue.queue))
print("Middle index for pop:", (len(queue.queue) - 1) // 2)
Step by step:
Initial queue: [50, 30, 40, 10, 20]
Length: 5
Middle index for pop: 2

Key Points

  • Uses Python's insert() method for positioning elements
  • Middle calculations handle both odd and even length queues
  • Returns None when trying to pop from an empty queue
  • Time complexity: O(n) for front/middle operations, O(1) for back operations

Conclusion

This flexible queue implementation provides complete control over element positioning using Python lists. The middle position calculations ensure proper behavior for queues of any length, making it suitable for applications requiring dynamic access patterns.

Updated on: 2026-03-26T14:00:11+05:30

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements