Skip to content

Instantly share code, notes, and snippets.

@devpeds
Last active May 26, 2017 13:59
Show Gist options
  • Save devpeds/36486192a8b8ec012b4d372f68e2cc7a to your computer and use it in GitHub Desktop.
Save devpeds/36486192a8b8ec012b4d372f68e2cc7a to your computer and use it in GitHub Desktop.
JavaScript Data Structure - Stack
var Stack = function() {
this.data = [];
};
Stack.prototype.size = function() {
return this.data.length;
};
Stack.prototype.empty = function() {
return this.size() === 0;
};
Stack.prototype.push = function(val) {
this.data.push(val);
};
Stack.prototype.pop = function() {
return !this.empty() ? this.data.pop() : -1;
};
Stack.prototype.top = function() {
return !this.empty() ? this.data[this.data.length-1] : -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment