Session 12 |
Queue
A Queue
is an ordered data structure. Elements are appended to the end of the queue (add
) and are removed (remove
) from the beginning of the 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 a program flow (the user inputs are stored in the queue and later they are executed in FIFO order).
Session 12 |