-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.js
More file actions
45 lines (40 loc) · 850 Bytes
/
Queue.js
File metadata and controls
45 lines (40 loc) · 850 Bytes
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
class Node {
constructor(val){
this.val = val
this.next = null
}
}
export default class Queue{
constructor(){
this.first = null
this.last = null
this.length = 0
}
enqueue(val){
const node = new Node(val)
this.length++
if(this.length === 1){
this.first = node
this.last = node
}
else{
this.last.next = node
this.last = node
}
return val
}
dequeue(){
if(this.length === 0) return undefined
this.length--
const temp = this.first
if(this.length === 0) this.last = null
this.first = temp.next
return temp.val
}
clear(){
this.first= null
this.last = null
this.length = 0
return this
}
}