Stack is a linear data structure that is widely used in operating systems and many applications. The stack implies an office stack of files in which you put new files on the top and the employee always take care of the newly arrived file first.
Similarly, when a stack is implemented in whichever method you want it to, newly elements are added to or deleted from the top of the stack, and stack operations always take the top elements and work its way downwards until the stack is empty.
Two ways to implement a stack are:
- Arrays
- Linked Lists
How does a Stack Works
Stack follows a principle called Last in, First Out or LIFO.
The elements are added to the top or deleted from the top using stack operations.

In the figure above, we inserted a number 66 at the top of the Stack with push(66); method. When we want to delete from the stack, we can’t delete anywhere from the Stack. We can only delete element at the top of the Stack which is number 66.
To delete from the top, we can use pop(); method. The pop() does not take any parameters, it simply delete top of the Stack element.
Stack Operations
Stack operations are methods or functions that can add, delete or check status of stack. Here is a list.
- push();
- pop();
- peek();
- isEmpty;
Push()
This function or method will insert an element to the top of the Stack.
Suppose our stack is
let stack = [23, 44, 66];
stack.push(77);
console.log(stack);Pop()
This function deletes the topmost element from the Stack.
Suppose for a given stack.
stack = [43, 64, 88];
stack.pop();
console.log(stack); // returns [43, 64]Peek()
This returns the topmost element of the start with any modification. It is just to display or get the top of the stack element.
stack = [20, 30, 40];
stack.peek();
print(stack);// returns 40 isEmpty()
The isEmpty() method is to check whether stack is empty of not. It will return a Boolean value.
stack = [44, 88];
value = stack.isEmpty();
console.log(value) //returns False because stack is not empty.Note: all code examples are in JavaScript and only push() and pop() are built-in functions.
How to Implement Stack in JavaScript
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpty()) {
console.log("Stack Underflow");
return null;
}
return this.items.pop();
}
peek() {
if (this.isEmpty()) {
console.log("Stack is empty");
return null;
}
return this.items[this.items.length - 1]; // Access the last element
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}
// Usage example
const myStack = new Stack();
myStack.push(10);
myStack.push(20);
console.log(myStack.peek()); // Output: 20
myStack.pop();
console.log(myStack.peek()); // Output: 10Applications of Stack
Here I give you some programs to implement stack to solve a programming problem in different programming languages.
- Tower of Hanoi
- Implement Stack in C
- Implement Stack using Linked List
- Evaluating Postfix Expressions
- Convert Infix to Postfix Expressions
- Convert Infix to Prefix Expressions