PUBLISHED ON: FEBRUARY 22, 2023
JavaScript Program to Implement a Stack
Do you find yourself searching through lengthy lists to find the most recently added element? This is a common problem that developers face when working with data structures. Luckily, there's a simple solution—using a stack data structure. In this article, we'll dive into the world of stacks and explore how to create one using JavaScript.
We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.
What is Stack Data Structure?
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack and the only element that can be removed is the element that is at the top of the stack, just like a pile of objects.
- Stack is an ordered list of similar data type.
- Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
push()
function is used to insert new elements into the Stack and pop()
function is used to remove an element from the stack. Both insertion and removal are allowed at only one end of Stack called Top.
- Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty.
How to Implement Stack
In the program given below, the Stack
class is created to implement the stack data structure. The class methods like add()
, remove()
, peek()
, isEmpty()
, size()
, clear()
are implemented.
An object stack is created using a new
operator and various methods are accessed through the object.
- Here, initially, this.items is an empty array.
- The
push()
method basically adds an element to this.items.
- The
pop()
method removes the last element from this.items.
- The
length
property gives the length of this.items.
Program to Implement a Stack
?// JavaScript Program to implement stack data structure
class Stack {
constructor() {
this.items = [];
}
// adding of elements to the stack
add(element)
return this.items.push(element);
// removable of elements from the stack
remove() {
if(this.items.length > 0)
return this.items.pop();
}
// check if the stack is empty
isEmpty(){
return this.items.length == 0;
}
// returning the size of the stack
size(){
return this.items.length;
}
// viewing the last element
peek() {
return this.items[this.items.length - 1];
}
// empty the stack
clear(){
this.items = [];
}
}
let st = new Stack();
st.add(1);
st.add(2);
st.add(3);
st.add(5);
console.log(st.items);
st.remove();
console.log(st.items);
console.log(st.peek());
console.log(st.isEmpty());
console.log(st.size());
st.clear();
console.log(st.items);
[1, 2, 3, 5]
[1, 2, 3]
3
false
4
[]
Conclusion
In this blog, we've covered the fundamental concepts of the Stack, including its operations and the steps involved in implementing it using the JavaScript Language. With this knowledge under your belt, you'll be equipped to wield the Stack with confidence and efficiency, enabling you to take on a broad range of programming challenges.