Stacks in a nutshell
A stack is a linear data structure that uses the Last-In-First-Out (LIFO) principle. Think of a stack of plates that needs to be washed. You take the first plate from the top (which was the last one to be added) and you wash it. Afterward, you take the next plate from the top and so on. This is exactly what a real-life stack is (for example, a stack of plates, a stack of books, a stack of CDs, and so on).
So, technically speaking, in a stack, the elements are only added (known as the push operation) and removed (known as the pop operation) to/from one end of it (known as the top).
The most common operations that are performed in a stack are as follows:
push(E e)
: Adds an element to the top of the stackE pop()
: Removes the top element from the stackE peek()
: Returns (but doesn't remove) the top element from the stackboolean isEmpty()
: Returnstrue
if the stack is emptyint size()
: Returns the size of the stackboolean isFull...