用入栈和出栈的方法实现字符串的逆序输出

    //入栈和出栈方法的实现字符串的逆序输出
function stack() {
    this.data=[];
    this.top=0;
}
stack.prototype={
    push:function (element) {
        this.data[this.top++]=element;
    },
    pop:function () {
        return this.data[--this.top];
    },
    length:function () {
        return this.top;
    }
}
function reverseString(str) {
    var s=new stack();
    var arr=str.split('');
    var result='';
    for(var i=0;i<arr.length;i++){
        s.push(arr[i]);
    }
    for(var j=0;j<arr.length;j++){
        result+=s.pop(i);
    }
    return result;
}

猜你喜欢

转载自blog.csdn.net/lgl_19910331/article/details/82385384