Exploring a possible solution
Now that we know that some problems can only be solved by processing data items in a FIFO order, we can look at a data structure that inherently maintains this order by design. A queue is a linear container that has two open ends. It allows data to be added at one end and removed from another. This condition imposed on adding and removing items from two different ends ensures that what goes first in the queue comes out first (FIFO).
Queue of people
A real-life example is a queue of people standing at the ticket counter to get tickets for a movie. A new person who wants to watch the movie joins the queue at the end. Similarly, the ticket counter only serves the person standing at the front of the queue. When a person gets their ticket, they walk out of the queue from the front, and the second person is at the front, ready to be served.
Liking the course? Check our discounted plans to continue learning.