-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.js
More file actions
42 lines (41 loc) · 726 Bytes
/
Queue.js
File metadata and controls
42 lines (41 loc) · 726 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
class Node {
constructor(value,next= null){
this.value = value
this.next = next
}
}
class Queue {
constructor(){
this.head = null
this.tail = null
this.size = 0;
}
enqueue(value){
let node = new Node(value)
if(!this.head){
this.head = node
this.tail = node
return ++this.size
}
this.tail.next = node
this.tail = node
return ++this.size
}
dequeue(){
if (!this.head){
return null;
}
let temp = this.head
if (this.head === this.tail){
this.tail = null
this.head = null
return --this.size
}
this.head = this.head.next
return --this.size
return temp
}
peek(){
return this.head.value
}
}