def enQueue(i) if (isFull()) print("Queue is Full\n") else $queueArray.unshift(i) print("Inserted the element ",i," to the Queue\n") end end def deQueue() element = 0 if (isEmpty()) print("Queue is empty\n") return -1 else element = $queueArray.pop print("The element ",element," is removed from the Queue\n") return element end end def isFull() if ($queueArray.length == $SIZE) return true else return false end end def isEmpty() if ($queueArray.length == 0) return true else return false end end def front() if (isEmpty()) print("The Queue is empty") return -1 else return $queueArray[$queueArray.length-1] end end def rear() if (isEmpty()) print("The Queue is empty") return -1 else return $queueArray[0] end end $SIZE = 10; $queueArray = [] 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\n") else $queueArray.unshift(i) print("Inserted the element ",i," to the Queue\n") end end
Now, the enQueue(i) method calls the isFull() method to check if the Array is full or not.
if (isFull()) print("Queue is Full\n")
In this case the Array is not full, so we come to the else part.
else $queueArray.unshift(i) print("Inserted the element ",i," to the Queue\n")
And the $queueArray.unshift(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.