17.1 What are they?

17.1 What are they?


Stacks and Queues are some of the most basic data structures that are found in programming. While they have a simple function, they are among the most powerful tools that are employed almost everywhere from front-end app designs to backend streaming applications. There are many different variants of Stacks and Queues but know just for now that these Stacks and Queues are primarily different in that they have different eviction policies.

Eviction Policies

An eviction policy defines the order in which the elements are removed from the data structure after they have entered it. For instance, there is the Priority Queue in Java, which evicts elements based upon their natural ordering rather than the sequence in which they entered it. I digress, however. For now, let us consider two basic policies, FIFO and FILO.


There are two: first-in-first-out (FIFO) found in queues and first-in-last-out (FILO) found in stacks. Note that in many programming languages, stacks are considered to be a specific type of Queue. For the rest of the lesson, unless specified, a queue follows the FIFO policy. There is a generalized version of Stacks and Queues which is called a Deque as well (where we can push and pop elements from both ends, typically implemented as a doubly-linked list).

java.util Imports

Before we dive into this topic, make sure to import these classes from the java.util package. If you don’t you will get an unnecessary compiler error.

Next Section

17.2 Queues
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.