Queue - Data Structure
Archived from my old blog, mikirinkode.online (2021–2022). Posted as it was written.
Contents
Hello there, in this article we will discuss what Queue is. For a list of Queue implementations, see here:
What is Queue?
Queue is a collection of data in which the addition of data or elements can only be done on the backside, while the deletion or removal of elements can only be done on the front side. So there are 2 gates in the queue, one gate is used for entry and the other is used for exit. This is the same as the concept of queuing which is usually done every day, where the first to queue, will be the first to come out then followed by the next, and so on.

If the Stack uses LIFO (Last In First Out) characteristics, this queue has a different characteristic, namely FIFO (First In First Out) which means that the data that enters first is the data that will go out first. This difference in characteristics matters as in the case when it comes to deleting elements, where on the Stack the deleted elements are the newly added elements, whereas in the queue the element that is removed is the last one added.
Operation on Queue
- Enqueue. This operation is used to add an element to the end of the queue. The addition operation can be performed as long as the queue is not full. Every time an enqueue occurs, the rear variable or the rear value indicator will be added by 1 (increment).
- Dequeue. This operation is used to remove the first element in the queue. Elements in a queue can be dequeued as long as the queue is not empty, if it is empty, underflow can occur. Because the deletion is done on the element that is in the first order or index, then after the deletion it is necessary to shift each element after it.
- Display / View. This operation is used to print or display all the data in the queue.
- Is Empty. This operation is used to check the condition of the queue whether it is empty or not. Because at the time of dequeue, it is necessary to ensure that the queue is still there / not empty so that underflow does not occur. A queue can be said to be empty if the back position pointer is equal to 0. This operation is usually made in the form of a function so that every time this function is called it will return a value according to the condition. If the queue is empty, the function will return true, otherwise, it will return false.
- Is Full. This operation is used to check whether the queue is full or not. Because in the enqueue process (adding value to the queue) it is necessary to ensure that the enqueue is not full so that there is no overflow. A queue can be said to be full if the back position pointer is equal to the maximum value of the queue. This operation is usually made in a function so that every time this function is called it will return a value according to the condition. If the queue is full then the function returns true, otherwise, it will return false and the program can enqueue.
- Destroy / clear. This operation is used to delete or clean all data in the queue. This operation usually calls the is Empty function to ensure that the queue contains values or data that can be deleted. If empty it will display a message that the queue is still empty.
- Peek. This operation is used to get the value of the first element in the queue without changing
Queue Implementation using Array
Queue can be implemented with an array or linked list. Implementation of queues with arrays needs to pay attention to the indexes, namely front and rear so that it can be seen whether the queue is empty or not. The queue can be added with a new element as long as the position of the rear is not equal to the maximum index of the queue array. To dequeue an array usually by taking or removing the element value at index 0, then shifting the next value. If you want to enqueue an array, you need to find the index where the value of the last element is.
Queue Implementation using Linked List
Queue implementation using a linked list has advantages when compared to arrays, namely queues can be done dynamically by using pointers.
Conclusion
Queue is a data structure that contains a collection of data where this Queue is FIFO (First In First Out) i.e the first data to enter will be the first to leave. Thank you for reading :)
Keep writing code and Have a nice day!