Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - QUEUES CODE




Implementing Queue in Python using Arrays


Example :



SIZE = 10;
queueArray = []

def enQueue(i):
	if (isFull()):
		print("Queue is Full") 
	else:
		queueArray.append(i)
		print("Inserted the element ",i," to the Queue")

def deQueue():
	element = 0
	if (isEmpty()):
		print("Queue is empty")
		return -1
	else:
		element = queueArray.pop(0)
		print("The element ",element," is removed from the Queue")
		return element
    
def isFull():
	if (len(queueArray) == SIZE):
		return True
	else:
		return False

def isEmpty():
	if (len(queueArray) == 0):
		return True
	else:
		return False

def front():
	if (isEmpty()):
		print("The Queue is empty")
		return -1
	else:
		return queueArray[len(queueArray)-1]

def rear():
	if (isEmpty()):
		print("The Queue is empty")
		return -1
	else:
		return queueArray[0]

enQueue(10)
enQueue(20)
enQueue(30)

deQueue()
deQueue()
deQueue()

deQueue()


Output :



  Inserted the element 10 to the Queue
  Inserted the element 20 to the Queue
  Inserted the element 30 to the Queue
  The element 10 is removed from the Queue
  The element 20 is removed from the Queue
  The element 30 is removed from the Queue
  Queue is empty

Code Explanation for Queue


So, what we have tried in the above code is, we have inserted the the elements 10, 20 and 30 to the Queue.


Then we take the elements 10, 20 and 30 from the Queue.


As we know the most important operations/methods of Queue are Enqueue and Dequeue. However, there are other methods that we will be using here.


So, we have the below methods :

  1. def enQueue(i)

    The enQueue(i) method puts an element into the Queue.

  2. def deQueue()

    The deQueue() method takes an element out of the Queue.

  3. def front()

    The front() method returns the first element of the Queue.

  4. def rear()

    The rear() method returns the last of the Queue.

  5. def isEmpty()

    The isEmpty() method returns true if an array is empty and returns false if the array is not empty.

So, at first, we define the array where we will be storing the elements of the Stack.


queueArray = []

Also, we have defined a constant called SIZE where we have defined the size of the array.


SIZE = 10;

Now, let us go to the main program and see the Queue in action.


So, initially we have an empty Queue.

java_Collections
java_Collections

Then we try to insert the element 10 into the Queue, using EnQueue operation,


enQueue(10);

So, the enQueue(i) method is called,


def enQueue(i):
	if (isFull()):
		print("Queue is Full")
	else:
		queueArray.append(i)
		print("Inserted the element ",i," to the Queue")

Now, the enQueue(i) method calls the isFull() method to check if the Array is full or not.


if (isFull()):
	print("Queue is Full")

In this case the Array is not full, so we come to the else part.


else:
	queueArray.append(i)
	print("Inserted the element ",i," to the Queue")

And the queueArray.append(i) will insert the element to the Queue.

java_Collections
java_Collections

Then we come to the next line and try to insert the next element 20 to the Queue.


enQueue(20)

So, the enQueue(i) method is called,


Similarly, the number 20 gets inserted to the Queue.

java_Collections
java_Collections

And similarly the other operations happens.