Verem adatszerkezet JS Objektum

let ml = {
    push( k ) {
        this.begin = { key: k, next: this.begin }
    },
    pop() {
        if ( this.pv = this.begin ) {
            this.begin = this.begin.next
            return this.pv.key
        }
    },
    forEach( f ) {
        let e = this.begin, i = 0
        while (e) f( e.key, i++, e ), e = e.next
    }
}

ml.push(1); ml.push(2); ml.push(3); ml.pop()
console.log( ml )
ml.forEach( v => console.log(v) )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Verem adatszerkezet JS Osztály

class ML {
    push( k ) {
        this.begin = { key: k, next: this.begin }
    }
    pop() {
        if ( this.pv = this.begin) {
            this.begin = this.begin.next
            return this.pv.key
        }
    }
    forEach( f ) {
        let e = this.begin, i = 0
        while (e) f( e.key, i++, e ), e = e.next
    }
}
const ml = new ML
ml.push(1); ml.push(2); ml.push(3); ml.pop()
console.log( ml )
ml.forEach( v => console.log(v) )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Utoljára frissítve: 2/23/2020, 10:49:47 AM