-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfifo_queue.js
68 lines (55 loc) · 1.03 KB
/
fifo_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
function FifoQueue(){
let head = undefined;
let tail = undefined;
let length_ = 0;
this.push = function(element){
let node = {
next: undefined,
element: element
}
if (head !== undefined)
head.next = node;
else
tail = node;
head = node;
length_++;
}
/** Peek first element and remove from queue
*/
this.pop = function(){
if (tail === undefined){
console.log("cant pop empty queue");
return undefined;
}
let last = tail;
if (head === tail)
head = undefined;
tail = last.next;
length_--;
return last.element;
}
/** Peek first element
*/
this.peek = function(){
if (tail === undefined){
console.log("cant peek empty queue");
return undefined;
}
return tail.element;
}
this.length = function(){
return length_;
}
this.contains = function(element){
if (tail === undefined){
return false;
}
let currentNode = tail;
while (currentNode !== undefined){
if (currentNode.element == element){
return true;
}
currentNode = currentNode.next;
}
}
}