Skip to content

Instantly share code, notes, and snippets.

@strawberryjello
Created January 29, 2015 04:05
Show Gist options
  • Save strawberryjello/ccbe8fc2d55bccdb7b69 to your computer and use it in GitHub Desktop.
Save strawberryjello/ccbe8fc2d55bccdb7b69 to your computer and use it in GitHub Desktop.
Sample implementation of a stack in JavaScript.
// see: http://jsfiddle.net/r5p9Lbj5/
function Node(value) {
this.bottom = null;
this.value = value;
}
function Stack() {
this.top = null;
}
Stack.prototype.push = function(value) {
var newTop = new Node(value);
newTop.bottom = this.top;
this.top = newTop;
};
Stack.prototype.pop = function() {
var oldTop = this.top;
this.top = oldTop.bottom;
return oldTop.value;
};
Stack.prototype.isEmpty = function() {
return this.top == null;
};
var s = new Stack();
s.push(1);
s.push(5);
s.push('derp');
alert(s.pop());
alert(s.pop());
alert(s.pop());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment