Updated April 13, 2023
Introduction to Queue in Python
Queue are the containers for storing data items that are waiting to be retrieved. One can control and manipulate data through queues. There are four types of queues:
- FIFO
- LIFO
- Priority Queue
- Circular Queue
There are two basic operations carried out in “Queue”:
- Enqueue is known as insertion.
- Dequeue is known as deletion.
A queue is open at both ends. The element which enters first is the one that comes out first from the queue. Random addition or deletion is not allowed in between queues. Queue helps you making the flow of tasks and keep track of it.
General Queue Algorithm
Below are the points in the general queue algorithm:
1. Declare a list of elements and maximum size of the Queue
2. Set head and tail of queue to 0
3. Number of elements in the queue -> Size = Tail – Head
4. Enqueue operation:
- Now check, if Size < MaxSize:
- If yes: Append data to Queue and then increment Tail by 1
- If it’s no, print message: Queue full
5. Dequeue operation
- Check if Size > 0:
- If yes: Pop the first element from the list and then increment Head by 1
- If no: Call Reset method. Print message: Empty message
Python provides it in the form of module name “queue”, which can be imported to the python environment and used directly.
Examples to Implement Queue in Python
Below are examples mentioned:
Example #1. FIFO
Here is the example of FIFO queue: Addition and deletion of one element in the queue:
Code #1
import queue
s = queue.Queue()
type(s)
Output:
FIFO is, by default, if queue type is not defined explicitly. Now let’s put something in a queue and see.
Code #2
import queue
s = queue.Queue()
type(s)
s.put(10)
s.get()
Output:
s.put() helps in keeping an element inside a queue. Whereas s.get() will help in retrieving an element from the queue. Addition and deletion of multiple elements in queue: Now, since we added elements in the queue, let’s see how it looks through the code below:
Code #3
import queue
s = queue.Queue()
for j in range(5):
s.put(j)
while not s.empty():
print(s.get())
print("ENDs")
Output:
Example #2. LIFO
Same addition and deletion can be done over LIFO as well. Here is the example of the LIFO(last in, first out) queue:
Code:
import queue
s = queue.LifoQueue()
for j in range(5):
s.put(j)
while not s.empty():
print(s.get())
print("ENDs")
Output:
Example #3. Priority Queue
This queue is a bit different from LIFO and FIFO queue. Here order is not a matter of interest.
Code:
import queue
s = queue.PriorityQueue()
s.put(2)
s.put(0)
s.put(1)
while not s.empty():
print(s.get())
print("ENDs")
Output:
So, as one can see, the lowest value items come out of the queue first. However, if there are two items holding the same value, then the order comes into consideration.
Example #4. Circular Queue
This is a type of queue, which is circular in shape. Here the end of the queue that is tail becomes the first of the queue that is head. This also works on the principle of “FIFO”. This is also called “Ring Buffer”. The application of the circular queue is mostly in the traffic system, CPU scheduling, etc.
Circular Queue Algorithm
Below we learn the algorithm circular queue.
- Initialize the queue with items. Set the size of the queue, i.e. maxSize, Initialize values for head and tail pointers.
enqueue: Check, if the number of elements = maxSize – 1:
- If Yes, then return message: Queue is full
- If No, then add the new data element to the location of the tail pointer. Increment the tail pointer.
dequeue: Check if the number of elements in the queue = 0:
- If Yes, then return message: Queue is empty
- If No, Increment the head pointer.
size:
- If, tail >= head, then size = tail – head
- If, head > tail, then size = maxSize – (head-tail)
Operations in Queue
Now let’s see some operations related to queue:
1. Queue empty()
In returns True or False based on any item available in the queue. If the queue holds some values, it returns True. If the queue holds no items, it returns False.
Code:
import queue
s = queue.PriorityQueue()
s.put(2)
s.put(0)
s.put(1)
s.empty()
Output:
2. Queue full()
It returns True if the queue is full. Else False.
Code:
import queue
s = queue.PriorityQueue()
s.put(2)
s.put(0)
s.put(1)
s.full()
Output:
3. Queue task_done: If tasks are enqueued and going on. In order to check if it’s done, this function is used. It basically helps in thread-handling.
4. Qyeue qsize(): It returns the size of the queue.
Code:
import queue
s = queue.PriorityQueue()
s.put(2)
s.put(0)
s.put(1)
s.qsize()
Output:
5. Queue put(): It puts an item in the queue.
6. Queue get():> This function get() is use to remove item from queue.
“Collections.deque” and “multiprocessing.queue” are two more good python module which can be explored for queues.
Example of Multiprocessing.Queue
This is a type of queue where items need to be processed in parallel mode. It tends to share data among processes.
Python Code
From multiprocessing import Queue
Code #1
qq = Queue()
qq.put('dance')
qq.put('play')
qq.put('read')
qq.put('sleep')
qq
Output:
Code #2
qq = Queue()
qq.put('dance')
qq.put('play')
qq.put('read')
qq.put('sleep')
qq.get()
Output:
Code #3
qq = Queue()
qq.put('dance')
qq.put('play')
qq.put('read')
qq.put('sleep')
qq.get()
qq.get()
Output:
Code #4
qq = Queue()
qq.put('dance')
qq.put('play')
qq.put('read')
qq.put('sleep')
qq.get()
qq.get()
qq.get()
Output:
Code #5
qq = Queue()
qq.put('dance')
qq.put('play')
qq.put('read')
qq.put('sleep')
qq.get()
qq.get()
qq.get()
qq.get()
Output:
Conclusion – Queue in Python
The above covered is one of the most important concepts in the data structures of python. We discussed the types of queues and their operations, which should help you get a good grip over them and moreover to understand their real use cases. Stacks and queue are the most widely used data structures in the real world. After a good understanding of this, one will feel confident to solve data storage inefficiencies effectively.
Recommended Articles
We hope that this EDUCBA information on “Queue in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.