The Stack follows the LIFO principle, it means Last In First Out which works as the last added
element to the stack collection is available to remove first. It is like disks are placed one over
another in a single pile.
It has the following methods:
- Push() method to add the elements
- Pop() method to remove the elements on a stack
- Peek () method to read the last element.
Example 1: Stack collection adding and removing the element.
The output will be: