Stack in Javascript

Stack in Javascript

What is Stack?

A stack is an ordered collection of items that follow the (LIFO) Last In First Out principle. The addition and removal of items take place at the same end i.e. at the top. The newest elements are at the top and the oldest elements are at the bottom.

List of operations performed on Stack

push(): Adds a single or multiple items to the top of the Stack.

pop(): Removes and Returns the top item of the Stack.

peek(): Returns the top item of the Stack.

isEmpty(): Returns True if Stack is empty, False otherwise.

clear(): Removes all the items of the Stack.

size(): Returns the length of the stack.

Implementation of Stack using classical approach i.e. without using built-in methods of the array.

function Stack() {
  var items = [];
  var top = 0;
  //other methods go here

  //Push an item in the Stack
  this.push = function (element) {
    items[top++] = element;
  };
  //top++, first performs the operation then increment's the value

  //Pop an item from the Stack
  this.pop = function () {
    return items[--top];
  };
  //--top, first decrements the value then performs the operation

  //Peek top item from the Stack
  this.peek = function () {
    return items[top - 1];
  };

  //Is Stack empty
  this.isEmpty = function () {
    return top === 0;
  };

  //Clear the Stack
  this.clear = function () {
    top = 0;
  };

  //Size of the Stack
  this.size = function () {
    return top;
  };
}

Drivers code -

//creating new instance of Stack
var stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.size());
console.log(stack.pop());
console.log(stack.size());
stack.clear();
console.log(stack.isEmpty());

/*
Output:
3
false
3
3
2
true 
*/

Implementation of Stack using built-in array methods

function Stack() {
  var items = [];
  //other methods go here

  //Push an item in the Stack
  this.push = function (element) {
    items.push(element);
  };

  //Pop an item from the Stack
  this.pop = function () {
    return items.pop();
  };

  //Peek top item from the Stack
  this.peek = function () {
    return items[items.length - 1];
  };

  //Is Stack empty
  this.isEmpty = function () {
    return items.length === 0;
  };

  //Clear the Stack
  this.clear = function () {
    items.length = 0;
  };

  //Size of the Stack
  this.size = function () {
    return items.length;
  };
}

Drivers code -

//creating new instance of Stack
var stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.size());
console.log(stack.pop());
console.log(stack.size());
stack.clear();
console.log(stack.isEmpty());

/*
Output:
3
false
3
3
2
true 
*/

Implementation of Stack using ES6 class syntax

class Stack {
  constructor() {
    this.items = [];
  }

  //Push an item in the Stack
  push(element) {
    this.items.push(element);
  }

  //Pop an item from the Stack. Another way of defining method in class.
  pop = function () {
    return this.items.pop();
  };

  //Peek top item from the Stack.
  peek = function () {
    return this.items[this.items.length - 1];
  };

  //Is Stack empty
  isEmpty() {
    return this.items.length === 0;
  }

  //Clear the Stack
  clear() {
    this.items.length = 0;
  }

  //Size of the Stack
  size() {
    return this.items.length;
  }
}

Drivers code -

//creating new instance of Stack
var stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.size());
console.log(stack.pop());
console.log(stack.size());
stack.clear();
console.log(stack.isEmpty());

/*
Output:
3
false
3
3
2
true 
*/

Final Words

And that’s it for this blog.

I hope you’ve found this blog a good referencing resource and thank you for reading.

If this blog was helpful, please do like, comment and share. Thanks, see you in the next blog.✌️