Stacks are an elementary Data Structure where all interaction with the data is done through the looking at the first element. Stacks are last in first out (LIFO) data structures. It is named on the basis of how the elements are removed from it. Opposite to stack is queue, in which the last element is removed, the element which was most old item, and hence it is FIFO or first in first out data structure.
Stack Operations
There are two main operations on Stacks, Push and Pop.
- Push - The push operation is used to insert an item into the stack. The name push comes from the fact that when an item is inserted into the stack it is put into the first element in the stack, so we think of it as lying on top of the stack.
- pop - The pop operation is used to remove an item from the stack. Items are removed from the top of the stack first.
Other operations
- isEmpty - Tells whether stack is empty or not
- **top or peek **- It returns the most recently added element to the stack but dont remove it.
- isFull - If stack has array implementation, you can find this method as well.
Stack implementation
There are 2 ways in which stack can be implemented:
Applications of Stack