leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit dde83509e906793b29a4acd1950ffa9554462083
parent 970ec378e85247c562015ac6aae8854e397c0bfa
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Thu, 27 Apr 2023 09:29:08 -0500

Completed implement stack using js

Diffstat:
Aimplement-stack-using-queues/stack-using-queues.js | 32++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+), 0 deletions(-)

diff --git a/implement-stack-using-queues/stack-using-queues.js b/implement-stack-using-queues/stack-using-queues.js @@ -0,0 +1,32 @@ +//This is a simple javascript class that can be used to create a +//stack in javascript that employs the basid pop, push, empty, and top methods +//The time complexity of this code is O(n) where n is the number of requested actions to +//be ran on the list. +//Time: 52ms Beats: 81.38% +//Memory: 42.3MB Beats: 8.20% + +var MyStack = function() { + this.list = [null]; +}; + +MyStack.prototype.push = function(x) { + this.list.push(x); +}; +MyStack.prototype.pop = function() { + return this.list.pop(); +}; +MyStack.prototype.top = function() { + return this.list[this.list.length - 1]; +}; +MyStack.prototype.empty = function() { + if(this.list.length == 0){ + return true; + } + + for(let i = 0 ; i < this.list.length ; ++i){ + if(this.list[i] != null){ + return false; + } + } + return true; +};