Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 45180 / input.js
blob: 6974019e91f2feca639e84e70317474fc02311fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
export default class Queue {
    constructor() {
        this._firstNode = null;
        this._lastNode = null;
    }

    dequeue() {
        if (!this._firstNode) {
            return this._firstNode;
        }

        const payload = this._firstNode.payload;

        this._firstNode = this._firstNode.next;

        return payload;
    }

    enqueue(item) {
        const newNode = {
            payload: item,
            next: null
        };

        if (this._firstNode === null) {
            this._firstNode = newNode;
            this._lastNode = newNode;

        } else {
            this._lastNode.next = newNode;
            this._lastNode = newNode;
        }
    }

    clear() {
        this._firstNode = null;
        this._lastNode = null;
    }

    top() {
        if (!this._firstNode) {
            return this._firstNode;
        }

        return this._firstNode.payload;
    }

    isEmpty() {
        return !this._firstNode;
    }
}