What is Queue
A queue is a data structure where the data is organized in the form of a queue. We can understand a queue similar to a queue of men organized for voting in which the man who is standing first in the queue will first give the vote and then will leave the queue. The same thing happens in the case of a data queue where the element which is at the first position will be removed first and so on. Thus, the queue follows the FIFO principle, i.e., first-in, first-out. It means the element which arrived first will get removed from the queue first. Thus, the queue data structure is an ordered list of data values where the values are inserted from the end of the queue and are removed from the front side of the queue.
Implementing a Queue
Let's see an example through which we can understand the implementation of a queue:
In the above code, we have used the following three variables where each variable specifies their use as:
Therefore, similar to stack operations, there are also two main operations of a queue through which we can insert an element into the stack or remove an existing element from it. These operations are:
In respect to these main methods of the queue, there are some other methods also available that can be applied over the queue, which are:
We will discuss the practical implementation of these operations of a queue.
Implementing Queue Operations
Now, we will see the practical implementation of these queue operations, which are as follows:
1) enqueue (): The queue operation which is used for adding elements to the queue.
In the above code, we have added elements to the queue using the push function.
2) dequeue (): The queue operation which is used for removing or popping out the existing values from the queue.
In the above code, firstly, we have checked if the queue is already empty or not. It is because if the queue has no value, it will return "Underflow". Else, it will check and return the element.
3) Length (): The queue operation is used to return the length of the queue.
The statement this.rear will help to fetch the length of the queue.
4) isEmpty (): The queue operation is used to check whether the queue is empty or not. If the queue is found empty, it returns true. Otherwise, it returns false.
In the above code, it will check if the value of the rear, i.e., the end, is equal to 0 or not. If it is true, it will return true else false will be returned.
5) print (): The queue operation is used to print the elements of the queue from the index value 0 to the rear position of the queue.
In the above code, using for loop and beginning from the 0 indexes to the queue's rear position, it will print the value and put it in the data array.
6) clear (): The queue operation is used to clear or delete all the elements of the queue and makes the value of the rear equal to 0.
In the above code, using the clear () operation, the value in the data array becomes 0 and sets the rear value to 0.
The complete code:
Although the functionality of a queue is the same in every programming language, the use and syntax vary as per the programming language.