Stack
Stack
● We will explain the concept of stacks using an analogy. You must have seen a pile of plates
where one plate is placed on top of another as shown in Fig.
● Now, when you want to remove a plate, you remove the topmost plate first.
● Hence, you can add and remove an element (i.e., a plate) only at/from one position which is the
topmost position.
Push Operation
The push operation is used to insert an element into the stack.
The new element is added at the topmost position of the stack.
To insert an element with value 6, we first check if TOP=MAX–1. If the condition is false,
then we
increment the value of TOP and store the new element at the position given by stack[TOP].
Step 1, we first check for the OVERFLOW condition.
In Step 2, TOP is incremented so that it points to the next location in
the array. In
Step 3, the value is stored in the stack at the location pointed by TOP.
Pop Operation
● The pop operation is used to delete the topmost element from
the stack.
● To delete the topmost element, we first check if TOP=NULL.
● If the condition is false, then we decrement the value pointed by
TOP.