A stack is a simple algorithm which is used in our everyday tasks, such as listening to music or web browse history.
This data structure follows the "last in, first out" order, where the first element that was inserted into this particular stack will be the last element to exit it (since every other element has been added on top of it), and if an element was added at last, it will then be the first out of that stack (because that element was added at the top of the stack).
This algorithm consists of five main operations:
- Push: Adds an element to the top of the stack;
- Pop: Removes an element from the top of the stack;
- Peek/Top: Return the element at the top of the stack, without removing it;
- Size/Len: Return the number of elements of that stack;
- isEmpty: Verify if the stack is currently empty.
- Every time you create any data structure, you must plan on what and how to handle the elements of the data structure internally. One of the most popular ways is with an array or a container, since we will be inserting/appending these elements into some sort of storage type...