Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




QUEUES


If we have a look at the dictionary, The meaning of 'Queue' is, 'A line or sequence of people or vehicles awaiting their turn to be attended to or to proceed.'


Also in real life, we have often heard the term 'Queue'. i.e. A long 'queue' to pay electricity bills or a 'queue' to get movie tickets.


Even the concept of 'Queue' in here, is also the same.


Say for example, you came to get the Movie tickets and you find a long 'Queue'. So, you go and join the 'Queue'. And you are the last person in the 'Queue'.


And it is quite obvious that you have to wait for the people, who are in front of you. i.e. They should get their tickets first. And then you can get yours.


And the same concept applies applies here. The object that enters the Queue first, leaves the 'Queue' first. The 'Queue' follows the principle of FIFO(First In First Out).


The Operation of putting an Object into the Queue is called 'enqueue'.


And the Operation of taking an Object out of the Queue is called 'dequeue'.


Let us look at the below example, to understand 'Queue' in detail.


Let us say we have three Objects. The Objects could be anything, a ball, a toy or even a cup.


java_Collections

Now, let us put these three objects in the 'Queue', one by one.


Below we have the empty 'Queue'.


java_Collections

  1. So, at first we will insert 'object1' into the 'Queue'.

    Enqueue object1

    java_Collections


    And 'object1' would be added at the end of the 'Queue'.
  2. Then we insert 'object2' into the 'Queue'.

    Enqueue object2

    java_Collections


    And 'object2' would be added at the end of the 'Queue'. Now, the front element of the queue is 'object1' and last element is 'object2'.
  3. Finally, we insert 'object3' into the 'Queue'.

    Enqueue object3

    java_Collections


    And 'object3' would be added at the end of the 'Queue'. Now, the front element in the Queue is 'object1' and last element is 'object3'.

So, all the three objects are inserted into the 'Queue'.


java_Collections

Now, let us try to take out all the three Objects out of the 'Queue'.


And, as we know, the Operation of taking an Object out of the Queue is called 'dequeue'.


  1. Let us run the dequeue operation on the Queue.

    Dequeue

    java_Collections


    And the first object, i.e. 'object1' that was at the front of the 'Queue' is taken out with 'Dequeue' operation.
  2. Again, if we run the 'Dequeue' operation, any guesses which value would be taken out?

    Dequeue

    java_Collections


    'object2' that was at the front of the 'Queue' is taken out.
  3. And we just have one element left in the 'Queue', i.e. 'object3'. So, if we run 'Dequeue' operation again.

    Dequeue

    java_Collections


    'object3' would be taken out of the 'Queue' leaving the 'Queue' empty.


Methods/Functions used by Queue


Queue mainly uses the below six methods or functions :


  1. enQueue(queue, element) -Contains two parameters. i.e. 'queue' which is the Queue name and 'element' that is the actual element that will be put at the end of the Queue.
  2. deQueue(queue) -Contains only one parameter. i.e. 'queue' which is the Queue name. This method removes the last element from the Queue.
  3. front(queue) - Contains only one parameter. i.e. 'queue' which is the Queue name. This method returns the front element from the Queue without removing it.
  4. rear(queue) - Contains only one parameter. i.e. 'queue' which is the Queue name. This method returns the last element of the Queue.
  5. isFull(queue) -Contains only one parameter. i.e. 'queue' which is the Queue name. This method indicates if the Queue is full or not.
  6. isEmpty(queue) -Contains only one parameter. i.e. 'queue' which is the Queue name. This method indicates if the Queue is empty or not.