In Push operation, we add an element to the top of the stack. The stack follows the Last In First Out (LIFO) principle, meaning the last element added is the first one to be removed.
Adding an element to the top of the stack.
Before adding an element to the stack, we check if the stack is full.
If the stack is full (top==capacity-1), we get a Stack Overflow and cannot add any more elements.
If the stack isn't full, we increase the top by 1 (top = top + 1) and insert the new element at that position.
We can keep adding elements until the stack reaches its capacity.
In Peek or Top operation, we view the top element of the stack without removing it. This is useful when we want to check what the last added element is without modifying the stack.