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
| class Node { constructor(data = -1, next = null) { this.data = data this.next = next } } class Queue { constructor() { this.top = null this.bottom = null this.elems = 0 }
add(elem) { if (this.elems == 0) { this.top = this.bottom = new Node(elem) } else { let newNode = new Node(elem) this.bottom.next = newNode this.bottom = newNode } this.elems++ }
out() { if (this.elems == 0) { console.log("queue is empty") return } let current = this.top let value = current.data this.top = this.top.next this.elems-- this.current = null return value } }
|