Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Stack, Queue in Python using Module queue

Last Updated on August 30, 2022 by Gokul Kannan

In python, it is quite easy to implement stack and queue data structures. Stack works on the principle of LIFO (Last in First out) i.e. element which is inserted in last will be removed first. And queue works on the principle of FIFO (First in First out) i.e. element which is inserted first will be removed first. In Python, there are inbuilt functions and classes which will make our code shorter and simple.

The queue module is used to implement multi-producer, multi-consumer queues and it is very useful in threaded programming when we have to exchange the information safely between multiple threads. The Queue class in this module implements all the required locking semantics and it depends on the thread support’s availability in Python.
The queue module has three types of queue implementation, which differ only in the order in which the entries are retrieved. For the FIFO queue, the first element added is the first element removed and for the LIFO queue(it works like a stack), the most recent element added will be the first element which will be removed. And for priority queue, elements are kept in sorted order (using the heapq module) and the lowest valued element will be removed first.

Classes and exceptions in the queue module is as follows:

class queue.Queue(maxsize = 0)

This is a constructor for a FIFO queue. Argument maxsize is an integer which sets the upper bound limit on the number of elements that can be placed in the queue. Insertion operation will be blocked once this size has been reached, until the queue elements are consumed. If the value of maxsize will be less than or equal to zero then the size of the queue will be infinite.

class queue.LifoQueue(maxsize=0)

This is a constructor for a LIFO queue. Argument maxsize is an integer which sets the upper bound limit on the number of elements that can be placed in the queue. Insertion operation will be blocked once this size has been reached, until the queue elements are consumed. If the value of maxsize will be less than or equal to zero then the size of the queue will be infinite. This constructor works the same as stack data structures.

class queue.PriorityQueue(maxsize=0)

This is a constructor for a priority queue. Argument maxsize is an integer which sets the upper bound limit on the number of elements that can be placed in the queue. Insertion operation will be blocked once this size has been reached, until the queue elements are consumed. If the value of maxsize will be less than or equal to zero then the size of the queue will be infinite.

exception queue.Empty

This exception will be raised when non-blocking get() (or get_nowait()) is called on a queue object which is empty.

exception queue.Full

This exception will be raised when non-blocking put() (or put_nowait()) is called on a queue object which is full.

Queue Objects

Queue.qsize()

This function returns the approximate size of the queue.

Queue.empty()

This function will return True if the queue is empty otherwise False. If empty() returns True it will not guarantee that a subsequent call to put() will not block. Similarly(), if empty() returns False it will not guarantee that a subsequent call to get() will not block.

Queue.full()

This function will return True if the queue is full, else False will be returned. If full() returns True it will not guarantee that a subsequent call to get() will not block. Similarly, if full() returns False it will not guarantee that a subsequent call to put() will not block.

Queue.put(item)

This function will put an item into the queue.

Queue.get()

This function will remove and return an item from the queue.

Example Code of Queue:

import queue
Q = queue.Queue(maxsize=10)
Q.put(10)
Q.put(20)
Q.put(30)
Q.put(40)
print(Q.get())
print(Q.get())
print(Q.get())
print(Q.get())

Example Code of Underflow/Overflow:

import queue
Q = queue.Queue(maxsize=10)
print(Q.qsize())
Q.put(10)
Q.put(20)
Q.put(30)
Q.put(40)
print("Full: ", Q.full())
Q.put(50)
Q.put(60)
print("Full: ", Q.full())
print(Q.get())
print(Q.get())
print(Q.get())
print("Empty: ", Q.empty())
print(Q.get())
print(Q.get())
print(Q.get())
print("Empty: ", Q.empty())
print("Full: ", Q.full())

Example Code of Stack:

import queue
S = queue.LifoQueue(maxsize=10)

print(S.qsize())
S.put(10)
S.put(20)
S.put(30)
S.put(40)
S.put(50)
S.put(60)
print("Full: ", S.full())
print("Size: ", S.qsize())

print(S.get())
print(S.get())
print(S.get())
print(S.get())
print(S.get())
print("Empty: ", S.empty())

This article tried to discuss the concept of Stack, Queue in Python using Module queue. Hope this blog helps you understand the concept. To practice problems you can check out MYCODE | Competitive Programming at Prepbytes

Leave a Reply

Your email address will not be published. Required fields are marked *