-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
48 lines (40 loc) · 897 Bytes
/
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
module.exports = class MyQueue {
constructor() {
this.items = [];
}
// enqueue function
enqueue(element) {
// adding element to the queue
this.items.push(element);
}
// dequeue function
dequeue(element)
{
// removing element from the queue
// returns underflow when called
// on empty queue
if(this.isEmpty())
return "Underflow";
return this.items.shift();
}
// front function
front() {
// returns the Front element of
// the queue without removing it.
if(this.isEmpty())
return "No elements in Queue";
return this.items[0];
}
// isEmpty function
isEmpty() {
// return true if the queue is empty.
return this.items.length == 0;
}
// asString function
asString() {
let str = "";
for(let i = 0; i < this.items.length; i++)
str += this.items[i] +" ";
return str;
}
}