Javascript: Stack
/**
* STACK
*
*/
function Stack() {
this._top = null;
}
Stack.prototype = {
push: function(data) {
var node = {
data: data,
next: null
};
if(this._top) {
node.next = this._top;
}
this._top = node;
return this;
},
pop: function() {
var top = this._top,
data = top.data || null;
this._top = top.next || null;
return data;
},
toString: function() {
var node = this._top,
temp = [];
while(node) {
temp.push(node.data);
node = node.next;
}
if(temp.length) {
return temp.join(', ');
}
}
};