How To Implement Stack | In Javascript - letsbug
Stack an ordered collection of items in which insertion and deletions are allowed only at one end called the top of the stack. Today in this article we are seeing how to implement stacks in Javascript. But before that a little intro to stacks
Stack is most essential linear data structure. A stack is a non-primitive linear data structure. And most important all the deletion and insertion in a stack is done from top of the stack, the last added element will be the first element to be removed from the stack. That is the reason why stack is also called Last-In-First-Out(LIFO) type of list.
This is how to define it A stack is an ordered collection of homogeneous data elements where the insertion and deletion operation take place at only one end; called as top of the stack.And now
How to implement stack in javascript
To implement it we will use "class" to create a stack class in javascript. The stack class constructor has two properties that is items which is the list where the elements will be stored and the count to keep the record of element in the list. This method of implementing stack is not the only way of implementing it there are various ways you can implement it.
And there are various methods in the stack class they are:
- push
- This method is used to add or insert the element in the stack. As the count is initialize to zero. In this method we add the element and increase the count by one to track its length.
- the print method just loops through the stack element and print then to the screen.
- pop
- pop removes one element from the stack and returns it.
- size
- size method returns the size or length of the stack.
- peek
- As the name says its peeks the stack and returns the first element it sees or finds.
- clear
- To clear the stack clear methods is used.
class Stack { constructor() { this.items = new Array() this.count = 0 } push(element) { this.items[this.count] = element this.count++ } print() { let str = '' for (let i = 0; i < this.count; i++) { str += `${this.items[i]} \n` } console.log(str) return str } pop() { if (this.count == 0) { return undefined } else { let deletedItem = this.items[this.count - 1] this.count-- return deletedItem } } size() { return this.count } peek() { return this.items[this.count - 1] } clear() { return this.items = new Array() }}
Above code is of the stack class now let us test it and initialize it.
const stack = new Stack()
This is how to initialize a new class or object in javascript. Now we can call the methods of the stack class and use it.
stack.push(100)stack.push(200)stack.push(300)stack.print()console.log(stack.size())stack.pop()stack.print()console.log(stack.size())console.log(stack.peek())console.log(stack.clear())
we have called every methods that we created here. First we added 100, 200, 300 to the stack and printed it which would just print 100, 200, 300 on the screen.
Then called size and it returned us the size of the stack which is 3. Then coming to the pop which removed the last element from the stack so now we have only 100, 200 in the stack. So we print that and also check the size of that.
At the end we peek the stack which returns last element it see in the stack here 200 and at very end we clear the stack that means it has nothing it empty.
100
200
300
3
100
200
2
200
[ ]
Above is the output we would get.
Thankyou
Comments
Post a Comment