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




JAVA - SCRIPT-QUEUES - CODE




Implementing Queue in JavaScript using Arrays


Example :



<html>
<body>    
<script>

    function enQueue(i) {
        if (isFull()) {
            document.write("Queue is Full </br>") 
        }	
        else {
            queueArray.push(i)
            document.write("Inserted the element ",i," to the Queue </br>")
        }
    }		

    function deQueue() {
        var element = 0
        if (isEmpty()) {
            document.write("Queue is empty </br>")
            return -1
        }	
        else {
            element = queueArray.pop(0)
            document.write("The element ",element," is removed from the Queue </br>")
            return element
        }
    }		

    function isFull() {
        if (queueArray.length == SIZE) 
            return true
        else
            return false
    }		

    function isEmpty() {
        if (queueArray.length == 0) 
            return true
        else
            return false
    }		

    function front() {
        if (isEmpty()) {
            document.write("The Queue is empty </br>")
            return -1
        }	
        else
            return queueArray[queueArray.length-1]
    }		

    function rear() {
        if (isEmpty()) {
            document.write("The Queue is empty </br>")
            return -1
        }	
        else
            return queueArray[0]
    }		


    var SIZE = 10
    var queueArray = []

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

    deQueue()
    deQueue()
    deQueue()

    deQueue()

</script>
</body>
</html>


Output :



  Inserted the element 10 to the Queue
  Inserted the element 20 to the Queue
  Inserted the element 30 to the Queue
  The element 30 is removed from the Queue
  The element 20 is removed from the Queue
  The element 10 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. function enQueue(i)

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

  2. function deQueue()

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

  3. function front()

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

  4. function rear()

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

  5. function 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,


function enQueue(i) {
	if (isFull()) {
		document.write("Queue is Full </br>")
	}
	else {
		queueArray.push(i)
		document.write("Inserted the element ",i," to the Queue </br>")
	}
}

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


if (isFull()) {
	document.write("Queue is Full </br>")
}

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


else {
	queueArray.push(i)
	document.write("Inserted the element ",i," to the Queue </br>")
}

And the queueArray.push(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.