Session 13 |
Queue
A Queue
is an ordered data structure. Elements are appended to the end of a queue (add
) and are removed (remove
) from the beginning of a queue. Therefore, this data structure is also known as FIFO (first-in-first-out).
// cityToVisit of the Queue interface has functionality of the LinkedList Queue<String> cityToVisit = new LinkedList<>(); cityToVisit.add("Tartu"); // adds the element to the end of the list cityToVisit.add("Paide"); cityToVisit.add("Tallinn"); while (!cityToVisit.isEmpty()) { System.out.println(cityToVisit.remove()); // removes the first element of the list }
Queues are used in graphs and in the program flow control (user inputs are stored in a queue and later they are executed in FIFO order).
Session 13 |