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.
Now, let us put these three objects in the 'Queue', one by one.
Below we have the empty 'Queue'.
- So, at first we will insert 'object1' into the 'Queue'.
Enqueue object1
And 'object1' would be added at the end of the 'Queue'.
- Then we insert 'object2' into the 'Queue'.
Enqueue object2
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'.
- Finally, we insert 'object3' into the 'Queue'.
Enqueue object3
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'.
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'.
- Let us run the dequeue operation on the Queue.
Dequeue
And the first object, i.e. 'object1' that was at the front of the 'Queue' is taken out with 'Dequeue' operation.
- Again, if we run the 'Dequeue' operation, any guesses which value would be taken out?
Dequeue
'object2' that was at the front of the 'Queue' is taken out.
- And we just have one element left in the 'Queue', i.e. 'object3'. So, if we run 'Dequeue' operation again.
Dequeue
'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 :
- 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.
- deQueue(queue) -Contains only one parameter. i.e. 'queue' which is the Queue name. This method removes the last element from the Queue.
- 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.
- rear(queue) - Contains only one parameter. i.e. 'queue' which is the Queue name. This method returns the last element of the Queue.
- isFull(queue) -Contains only one parameter. i.e. 'queue' which is the Queue name. This method indicates if the Queue is full or not.
- isEmpty(queue) -Contains only one parameter. i.e. 'queue' which is the Queue name. This method indicates if the Queue is empty or not.