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()
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 :
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.
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.
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.
And similarly the other operations happens.