The stack data structure
Imagine you have a stack of trays in a cafeteria or food court, or a pile of books, as in the following image:

Now suppose you need to add a new book to the pile. The standard practice is to simply add the new book on the top of the pile of books. And in case you need to put the books back into the bookshelf, you would pick the book that is on the top of the pile, put it away, and then get the next book that is on the top until all the books have been stored away. This behavior of adding or removing books from the pile of books follows the same principle of a stack data structure.
A stack is an ordered collection of items that follows the last in, first out (LIFO) principle. The addition of new items or the removal of existing items takes place at the same end. The end of the stack is known as the top, and the beginning is known as the base. The newest elements are near the top, and the...