字符串反轉

class Stack {
    constructor() {
        this.dataSource = [];
        this.top = 0;
    }

    pop() {
        return this.dataSource[--this.top];
    }

    push(element) {
        this.dataSource[this.top++] = element;
    }

    peek() {
        return this.dataSource[this.top - 1];
    }

    clear() {
        this.top = 0;
    }

    length() {
        return this.top;
    }
}

let word = 'word';
let convert = '';
let _stack = new Stack();
for (let i = 0; i < word.length; i++) {
    _stack.push(word[i]);
}
for (let i = _stack.length(); i>0; i--) {
    convert += _stack.pop();
}
複製代碼
相關文章
相關標籤/搜索