[Algorithms] 17. Queue From Stacks

Mikyung Lee·2021년 2월 13일
0
post-thumbnail

문제


해결방법


제출 코드


class Queue {
    constructor() {
        this.first = new Stack();
        this.second = new Stack();
    }
    
    add(record) {
        this.first.push(record);
    }
    
    remove() {
        while (this.first.peek()) {
            this.second.push(this.first.pop());
        }
        
        const record = this.second.pop();
        
        while (this.second.peek()) {
            this.first.push(this.second.pop());
        }
        
        return record;
    }
    
    peek() {
        while (this.first.peek()) {
            this.second.push(this.first.pop());
        }
        
        const record = this.second.peek();
        
        while (this.second.peek()) {
            this.first.push(this.second.pop());
        }
        return record
    }
}
profile
front-end developer 🌷

0개의 댓글

관련 채용 정보