js实现栈

165 阅读1分钟
function Stack(){
}
Stack.prototype.push = function (v){
    this.head = {
        v:v,
        next:this.head
    };
};
Stack.prototype.pop = function () {
    if(this.head.next !== undefined){
        var top = this.head.v;
        this.head = this.head.next;
        console.log(top);
        return top;
    }else{
        console.log(this.head.v);
        return this.head.v;
    }
};