Queue
A Queue is a linear data structure that keeps its elements in a queue. It uses FIFO (first-in-first-out) ordering in which elements are added to the "end" and are removed from the "front".
Last updated
A Queue is a linear data structure that keeps its elements in a queue. It uses FIFO (first-in-first-out) ordering in which elements are added to the "end" and are removed from the "front".
Last updated
A Queue is a linear data structure that keeps its elements in a queue. It means that one end of a queue is used to add data into it and another end is used to remove data from it.
We often use queues in our everyday life. For example, queue at the supermarket, queue at the airport, and so on...
This ordering mechanism is also called FIFO (first-in-first-out) in which the first added element is processed first and the newest added element is processed last.
A Queue is like an array but with a few restrictions:
You can't randomly access an item by its index.
You can only add data in one end and retrieve data from the other.
It can be used for any task where the first data/job that arrives needs to be processed first (FIFO). For example, it's possible to use a queue to upload images one by one in the order they arrived.
*To access some value you need to remove elements that are first in a queue.
Operation
Description
enqueue(item)
add an item to the end of a queue
dequeue()
remove the first item in a queue
peek()
get an element that is in the front of a queue
Operation
Complexity
Insertion
O(1)
Deletion
O(1)
*Access
O(n)