Stack Operations
Here we explore the Four primary Stack Operations,
1. Push Operations
The Push Operations adds an element to the top of the stack. It involves placing the new element on top of the current top element. The stack grows as element are added.
Explanation
- Identify the top of the Stack.
- Place the new element on top.
- Update the top pointer to the new element.
2. Pop Operation
The Pop Operation removes the top element from the stack. It involves deleting the top element and then updating the top pointer to the next element below it. This operation reduces the size of the stack.
Explanation
- Identify the top element.
- Remove the top element.
- Update the top pointer to the next element.
3. Peek Operations
The Peek Operation retrieve the top element of the stack without removing it. This is useful for examining the top element without altering the stack structure.
Explanation
- Identify the Top Element.
- Return the Top Element.
4. isEmpty Operation
The isEmpty Operations Checks whether the stack is empty. Because, this is Returns a Boolean value: “true” if the stack contains no elements, and “false” otherwise.
Explanation
- Check if the top pointer is null (or if the stack size id zero, depending on implementations).
- Return “true” if the stack is empty; otherwise return “false”.