forked from vaishak10/DS-Algorithms-withJavascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
60 lines (49 loc) · 1.23 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
class Queue {
constructor(){
this.queue = [];
}
//Inserts a new element into the queue from the back of the queue
enqueue(value){
this.queue.push(value);
}
//Deletes a element from the front of the queue
dequeue(){
this.queue.splice(0,1);
}
//Return number of items in the queue
queueLength(){
return this.queue.length;
}
//Return first element in the queue
seek(){
return this.queue[0];
}
//View the elements in the queue
view(){
if(this.queue.length > 0)
{
return this.queue
} else {
console.log('Queue is empty.');
}
}
}
const queue = new Queue();
//Inserts elements 2,3,4 into the queue
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
console.log(queue.view());
//Deletes 2 from the queue
queue.dequeue();
console.log(queue.view());
//returns the length of queue
let queueLength = queue.queueLength();
if(queueLength) console.log("Length of the queue: " + queueLength);
//View the first element of the queue
let firstElement = queue.seek();
if(firstElement) {
console.log("First element of the queue: "+firstElement);
} else {
console.log('Queue is empty.');
}