Data Structures & Algorithms in JavaScript(Stack)

Hello Everyone, This is part 2 in the series of blogs about data structures and algorithms in JavaScript, In this blog, I will cover Stack data structure.



What is a Stack?

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). — geeksforgeeks.org
Real-World Example of the stack can be a pile of books, where books are placed one above another, books can be added or remove from the top of the pile of books






Stack of Books

List Of Operations Available


  1. Push : Insert an element in the stack.
  2. Pop   : Remove an element in the stack.
  3. Peek : Return the top element of the stack.
  4. Size :  Return Size of the stack.
  5. isEmpty : Check if the stack is empty if empty return true else false.
  6. Clear : Reset the stack. 

Implementation of Stack in JavaScript

There are two ways in which stack can implement in JavaScript one way by using Array or using JavaScript object( object is a set of key & value). As Array already have push method to insert an element at end of the array, a pop method to remove an element, to get the length of the array it has a property length which returns the size of the array if the length is equal to zero then the array is empty. you get the full source here

Implementation of Stack using JavaScript Objects

let’s define ES6 class name Stack, with two properties, count which will keep track of the number of elements in the stack and items an object which will store the elements as value and count as key.
class Stack {
    constructor() {
        this.count = 0;
        this.items = {};
    }
}

Push

To add an element to the stack we will use the count property as key for the items object & element as values. After pushing the element in the stack we will increment the count property by one.
we can only add new items to the top of the stack, meaning at the end of the stack
push(element) {
        this.items[this.count] = element;
        this.count++;
    }

Pop

While removing an element from the stack, there two cases:
  1. If the stack is empty, return undefined
  2. If the stack is not empty :
  3. Store the value of the top element i.e (count -1)
  4. decrease the count property by one
  5. delete element from items object and return the stored value.
As the stack uses the LIFO principle, the last item that we added is the one that is removed
pop() {
        if (this.isEmpty()) {
            return undefined;
        }
        let result = this.items[this.count-1];
        this.count --;
        delete this.items[this.count];        return result;
    }

Peek

If the stack is empty, return undefined else return the Top element i.e (count -1)
peek() {        if (this.isEmpty()) {
            return undefined;
        }
        return this.items[this.count-1];
    }

Size

Return count property, which keeps track of the number of elements in the stack.
size() {
        return this.count;
    }

isEmpty & Clear

isEmpty return boolean value, if the count property is zero then true else false. And to clear the stack, we can simply reset it to the same values we used in the constructor.
isEmpty() {
        return this.count == 0;
    } clear() {
    this.items = {};
    this.count = 0;
    return this.items;
    }
We could also use the following logic to remove all elements from the stack,
respecting the LIFO behaviour:
while (!this.isEmpty()) {
this.pop();
}
you get the full source here

Conclusion :

Stacks have a variety of applications in real-world problems. They can be used for backtracking problems to remember tasks or paths visited, and to undo actions. The complexity of stack is methods as follows.






Runtime Complexity of stack methods

So, stay tuned for the next blog, in which I will cover another DS Queue.
Data Structures & Algorithms in JavaScript(Stack) Data Structures & Algorithms in JavaScript(Stack) Reviewed by RS Coder on April 22, 2020 Rating: 5

No comments:

Powered by Blogger.