Created
August 10, 2023 18:06
-
-
Save Deshan555/9f8c9565c5f85a756979ab17e3e17c67 to your computer and use it in GitHub Desktop.
stack Implementation of Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Stacks Follow Princiles of : LIFO | |
| Push: Add an element to the top of a stack | |
| Pop: Remove an element from the top of a stack | |
| IsEmpty: Check if the stack is empty | |
| IsFull: Check if the stack is full | |
| Peek: Get the value of the top element without removing it | |
| */ | |
| class Stacks{ | |
| constructor(){ | |
| this.stack = []; | |
| this.size = 20; | |
| } | |
| push(data){ | |
| if(!this.stack.length <= 20){ | |
| this.stack.push(data); | |
| return this.stack; | |
| } | |
| return 'stack full'; | |
| } | |
| pop(){ | |
| this.stack.pop(); | |
| return this.stack; | |
| } | |
| get length_stack(){ | |
| return this.stack.length; | |
| } | |
| get peek(){ | |
| return this.stack[(this.stack.length)-1]; | |
| } | |
| get print(){ | |
| return this.stack; | |
| } | |
| } | |
| // stack operataions | |
| const stack = new Stacks(); | |
| console.log(stack.print); | |
| for (var i = 0; i < 10; i++) { | |
| stack.push(i); | |
| } | |
| console.log(stack.print); | |
| console.log(stack.pop()); | |
| console.log(stack.print); | |
| console.log(stack.length_stack); | |
| console.log(stack.peek); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment