Stacks
A stack is a data structure that has way more limitations, compared to arrays. We can add items to a stack only by adding them on top. And we can only remove the item on top of the stack.
The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out).
Implementing a Stack in JavaScript
- Push () → Add an element to the stack.
- Pop () → Delete an element from the stack.
- Peek () → Get the top element of the stack.
- Length () → Return the length of the stack.
- Search () → Search for the element in the stack.
- IsEmpty () → Check if the stack is empty.
- Print () → Print the elements of the stack.
Example :