Skip to main content

Pop and Peek Operations | Java Script

 

Lysecky states, "Pop and Peek operations should not be applied to an empty stack because the resulting behavior may be undefined."

In this Post, I will explain whether I think it's a problem and justify my answer with a citation from credible resources. Before we start, let's see what the Pop and Peek Operation is. According to an article from cs.lmu.edu, a stack implementation usually has three methods, push, pop, and peek. Push will let you add a single item onto the stack. Peek allows the user to see what value is on top of the stack, and pop allows the user to remove the top value from the stack.

I support lysecky's statement because the stack is an abstract structure in which a location is set aside to keep a stack of items, and we can get to elements one by one, starting from the top.

In technical terms, it follows LIFO or the last in, first out approach. So, in the stack, we can only get to the top item, and the last item we have added to it will be the first to get removed if we want to reach the previous item.

We can remove the item from one end called top

It has two main operation

Pop => it is for the removal of the item present at the top of the stack

peek => it returns the topmost element, but it won't remove it

 



Comments