Last active
September 4, 2016 09:47
-
-
Save alexhawkins/6f99b2dc0bdf2b507127 to your computer and use it in GitHub Desktop.
Stack Data Structures, Functional, Functional Shared, Prototypal, Pseudoclassical Implementations
This file contains 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
/**********FUNCTIONAL - SHARED STACK IMPLEMENTATION ****************/ | |
var makeStack = function() { | |
var stack = { | |
storage: {}, | |
length: 0 | |
}; | |
extend(stack, stackMethods); | |
return stack; | |
}; | |
//extend methods to our stack object | |
var extend = function(to, from) { | |
for (var key in from) { | |
to[key] = from[key]; | |
} | |
}; | |
var stackMethods = { | |
add: function(value) { | |
this.storage[this.length] = value; | |
this.length++; | |
}, | |
remove: function() { | |
if (this.size() > 0) { | |
var result = this.storage[this.length - 1]; | |
delete this.storage[this.length - 1]; | |
this.length--; | |
return result; | |
} else { | |
return null; | |
} | |
}, | |
size: function() { | |
return this.length; | |
} | |
}; | |
//TESTS | |
var newStack = makeStack(); //create stack instance | |
newStack.add(10); | |
newStack.add('dogs'); | |
console.log(newStack.storage); //{ '0': 10, '1': 'dogs' } | |
console.log(newStack.size()); //2 | |
newStack.add(20); | |
console.log(newStack.storage); //{ '0': 10, '1': 'dogs', '2': 20 } | |
newStack.remove(); | |
console.log(newStack.storage); //{ '0': 10, '1': 'dogs' } | |
newStack.remove(); | |
newStack.remove(); | |
console.log(newStack.storage); // {} | |
console.log(newStack.remove()); // null | |
/*****************FUNCTIONAL - FUNCTIONAL IMPLEMENTATION****************************/ | |
var makeStack = function() { | |
//set local variables to use throughout | |
var length = 0, | |
storage = {}, | |
stack = { | |
/*****METHODS*****/ | |
push: function(value) { | |
storage[length] = value; | |
length++; | |
}, | |
pop: function() { | |
if (length > 0) { | |
var result = storage[length - 1]; | |
delete storage[length - 1]; | |
length--; | |
return result; | |
} | |
}, | |
size: function() { | |
return length; | |
}, | |
storage: function() { | |
return storage; | |
} | |
}; | |
return stack; | |
}; | |
var stack = makeStack(); | |
stack.push(10); | |
stack.push(20); | |
console.log(stack.size()); //2 | |
console.log(stack.storage()); //{ '0': 10, '1': 20 } | |
/*****************FUNCTIONAL - PSEUDO-CLASSICAL IMPLEMENTATION****************************/ | |
//create stack constructor | |
var Stack = function() { | |
this.length = 0; | |
this.storage = {}; | |
}; | |
//create an add/push method for our prototype | |
Stack.prototype.add = function(value) { | |
this.storage[this.length] = value; | |
this.length++; | |
}; | |
//create a remove/pop method for our prototype | |
Stack.prototype.remove = function() { | |
//make sure item exists to remove | |
if (this.length > 0) { | |
//get last item | |
var result = this.storage[this.length - 1]; | |
delete this.storage[this.length - 1]; | |
this.length--; | |
return result; | |
} | |
}; | |
//let us check the size | |
Stack.prototype.size = function() { | |
return this.length; | |
}; | |
//create a new instance of Stack using the new | |
//to instantiate our class | |
var newStack = new Stack(); | |
newStack.add(10); | |
console.log(newStack); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment