How to implement a Queue data structure in Python

In this Python tutorial, we will learn how to implement a queue data structure in Python language. First, what is queue? A queue is a data structure which follows First In First Out (FIFO). That means the first element that is added to the queue is the first one to be removed.

 

Features of Queue in Python

1) A queue is an ordered list of elements

2) This data structure follows the FIFO order.

3) The deletion of the new element will be done only after all the previous elements of the new element are deleted.

 

                           Implementation of Queue in Python

 

Source code to implement a queue using Python

class queue: # Create a class queue

    def __init__(self, max_size, size=0, front=0, rear=0):
        self.queue = [[] for i in range(5)] #creates a list [0,0,0,0,0]
        self.max_size = max_size
        self.size = size
        self.front = front
        self.rear = rear
 
    # Methods of Queue
    
    def enqueue(self, data): # To enter the elements in a queue
        if not self.isFull():
            self.queue[self.rear] = data
            self.rear = int((self.rear + 1) % self.max_size)
            self.size += 1
        else:
            print('Queue is full')
 
    def dequeue(self): # To remove the elements in a queue
        if not self.isEmpty():
            print(self.queue[self.front], 'is removed')
            self.front = int((self.front + 1) % self.max_size)
            self.size -= 1
        else:
            print('Queue is empty')

    def isEmpty(self): # To check if the current queue is empty or not
        return self.size == 0

    def isFull(self): # To check if the current queue is full or not
        return self.size == self.max_size

    def show(self):
        print ('Queue contents are:')
        for i in range(self.size):
            print (self.queue[int((i+self.front)% self.max_size)])


            

q = queue(5)
print('Queue Insertion')
q.enqueue(1)
q.show()
q.enqueue(2)
q.show()
q.enqueue(3)
q.show()
q.enqueue(4)
q.show()
q.enqueue(5)
q.show(
print('Queue Deletion'))
q.dequeue()
q.show()
q.dequeue()
q.show()
q.dequeue()
q.show()
q.dequeue()
q.show()
q.dequeue()
q.show()

The methods implemented the queue are:-

  • enqueue():-  This method is used to add the elements at the bottom of the queue.
  • dequeue():-  This method is used to remove the elements at the top of the queue.
  • isEmpty():-   This method is used to check if the queue is empty or not.
  • isFull():-  This method is used to check if the queue is full or not.

In the above program, five elements are inserted into the queue. After entering the five elements, the removal of the elements is shown.

 

Output:-

Queue Insertion
Queue contents are:
1
Queue contents are:
1
2
Queue contents are:
1
2
3
Queue contents are:
1
2
3
4
Queue contents are:
1
2
3
4
5
Queue Deletion
1 is removed
Queue contents are:
2
3
4
5
2 is removed
Queue contents are:
3
4
5
3 is removed
Queue contents are:
4
5
4 is removed
Queue contents are:
5
5 is removed
Queue contents are:

 

You can also read,

Leave a Reply

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