Stacks are one of the most common data structures in computer science that one can consider. A stack is an ordered collection of homogeneous data elements,where the insertion and deletion takes place at one end, known as
top,which means the element which is inserted last must be deleted first.
The stack is also called as
LAST IN FIST OUT(LIFO).
Real-Life Applications of Stack:-
- Books, Clothes piled on top of each other
- Floors in a building.
- Browser back-forth button.
- Reversing a string
- Converting expressions(prefix-infix-postfix)
- Recursive functions.
- Maintaining any sort of LIFO data.
The basic operation that can performed are
PUSH(terminology for Insertion) - Inserting or adding the elments into the stack.It places the object on top of the stack.
POP(terminology for Deletion) - Deleting or removing an element from the stack.It removes an object from the top of the stack.
IsEmpty - Reports whether the stack is empty or not.
IsFull - Reports whether the stack exceeds limit or not.
Peek - returns the
top record without popping it.
Create a stack data structure.The stack should be a class with methods
peek.Adding an element to the stack should store it until it is removed.
const s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); //returns 3 s.pop(); // returns 2