forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueues.js
49 lines (34 loc) · 860 Bytes
/
queues.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
function Queue(){
//define array
var items = []
//methods to add and remove elements in a way that follow the FIFO principle
this.enqueue = function(...element){
items.push(element)
}
this.dequeue = function(){
return items.shift()
}
//to check for index 0 element
this.front = function(){
return items[0]
}
//check for the size of the queue
this.size = function(){
return items.length
}
//to check whether the queue is empty
this.isEmpty = function(){
return items.length === 0
}
//to clear the queue
this.clear = function(){
return items = []
}
this.print = function(){
console.log(items.toString())
}
}
var queue = new Queue()
queue.enqueue("John", "caleb")
console.log(queue.isEmpty())
queue.print()