- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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]