stack-using-queues.js (829B)
1 //This is a simple javascript class that can be used to create a 2 //stack in javascript that employs the basid pop, push, empty, and top methods 3 //The time complexity of this code is O(n) where n is the number of requested actions to 4 //be ran on the list. 5 //Time: 52ms Beats: 81.38% 6 //Memory: 42.3MB Beats: 8.20% 7 8 var MyStack = function() { 9 this.list = [null]; 10 }; 11 12 MyStack.prototype.push = function(x) { 13 this.list.push(x); 14 }; 15 MyStack.prototype.pop = function() { 16 return this.list.pop(); 17 }; 18 MyStack.prototype.top = function() { 19 return this.list[this.list.length - 1]; 20 }; 21 MyStack.prototype.empty = function() { 22 if(this.list.length == 0){ 23 return true; 24 } 25 26 for(let i = 0 ; i < this.list.length ; ++i){ 27 if(this.list[i] != null){ 28 return false; 29 } 30 } 31 return true; 32 };