
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to implement a queue that can push or pop from the front, middle, and back in Python
Suppose, we are asked to implement a queue that can push and pop values at the front, middle, and back.
We have to implement a pair of functions to push and pop for all three cases. We have to implement another function that shows the full queue at a given time.
So, if the input is like
push_from_back(10)
push_from_back(20)
push_from_front(30)
push_from_middle(40)
push_from_front(50)
show_queue()
pop_from_back()
show_queue()
pop_from_front()
show_queue()
pop_from_middle()
show_queue(),
then the output will be [50, 30, 40, 10, 20
[50, 30, 40, 10]
[30, 40, 10]
[30, 10]
To solve this, we will follow these steps −
array := array representation of the queue
Define a function push_from_front() . This will take value
insert value into array at position 0
Define a function push_from_middle() . This will take value
insert value into array at position (size of array) / 2
Define a function push_from_back() . This will take value
insert value at the end of array
Define a function pop_from_front() .
delete and return the first element from the array if it is not empty
Define a function pop_from_middle() .
delete and return the element at position (length of array - 1) /2
Define a function pop_from_back() .
delete and return the last element from the array
Define a function show_queue() . This will take no input
return array
Example
Let us see the following implementation to get better understanding
class Solution(): def __init__(self): self.array = [] def push_from_front(self, value): self.array.insert(0, value) def push_from_middle(self, value): self.array.insert(len(self.array) // 2, value) def push_from_back(self, value): self.array.append(value) def pop_from_front(self): return (self.array or [-1]).pop(0) def pop_from_middle(self): return (self.array or [-1]).pop((len(self.array) - 1) // 2) def pop_from_back(self): return (self.array or [-1]).pop() def show_queue(self): return self.array ob = Solution() ob.push_from_back(10) ob.push_from_back(20) ob.push_from_front(30) ob.push_from_middle(40) ob.push_from_front(50) print(ob.show_queue()) ob.pop_from_back() print(ob.show_queue()) ob.pop_from_front() print(ob.show_queue()) ob.pop_from_middle() print(ob.show_queue())
Input
ob = Solution() ob.push_from_back(10) ob.push_from_back(20) ob.push_from_front(30) ob.push_from_middle(40) ob.push_from_front(50) print(ob.show_queue()) ob.pop_from_back() print(ob.show_queue()) ob.pop_from_front() print(ob.show_queue()) ob.pop_from_middle() print(ob.show_queue())
Output
[50, 30, 40, 10, 20] [50, 30, 40, 10] [30, 40, 10] [30, 10]
- Related Articles
- queue::push() and queue::pop() in C++ STL
- queue::front() and queue::back() in C++ STL
- How to push and pop elements in a queue in Ruby?
- C# Program to Implement Stack with Push and Pop operations
- Reversing the alphabet from back to front and front to back in JavaScript
- Program to check given push pop sequences are proper or not in python
- Program to Implement Queue in Python
- Program to convert linked list by alternating nodes from front and back in Python
- Python Program to Implement Stack using One Queue
- C++ Program to Implement Queue
- stack push() and pop() in C++ STL
- C++ Program to Implement Queue in STL
- How to Implement Priority Queue in Python?
- Python Program to Implement Queue Data Structure using Linked List
- C++ Program to Implement Circular Queue
