-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
70 lines (62 loc) · 1.03 KB
/
queue.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Queue(){
this.head = null;
this.tail = null;
this.count = 0;
}
function Node(value){
this.value = value;
this.next = null;
}
Queue.prototype.enqueue = function(value){
let node = new Node(value);
if(!this.head){
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this.count++;
return this;
}
Queue.prototype.dequeue = function(){
if(!this.head){
return null;
}
let output = this.head;
this.head = this.head.next;
this.count--;
return output;
}
Queue.prototype.front = function(){
if(!this.head){
return null;
}
return this.head;
}
Queue.prototype.size = function(){
return this.count;
}
Queue.prototype.contians = function(value){
if(!this.head){
return null
}
let current = this.head;
while(current){
if(current.value == value){
return current;
}
current = current.next;
}
return null;
}
Queue.prototype.isEmpty = function(){
if(this.head){
return false
}
return true;
}
a = new Queue();
a.enqueue(1).enqueue(2);
a.dequeue();
console.log(a);