StackEasy

Push Operation

Stack Push Operation

Push Operation

The Push operation inserts a new element onto the top of the stack.

Key Rules

  1. Overflow Check: Before adding a new element, you must verify if the stack has reached its allocated capacity limit (top >= MAX - 1). If it is full, an Overflow condition is met, and the push is rejected.
  2. Increment Top: If space remains, increment the index pointer top by 1 to point to the next vacant slot.
  3. Store Value: Place the new element at the newly incremented index arr[top].

Visually

Before Push(40):             After Push(40):
  Index [3]: [ Empty ]         Index [3]: [  40   ] ◄top
  Index [2]: [  30   ] ◄top    Index [2]: [  30   ]
  Index [1]: [  20   ]         Index [1]: [  20   ]
  Index [0]: [  10   ]         Index [0]: [  10   ]