-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
62 lines (48 loc) · 1.43 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
//Programme to implement queue data structure
class Queue {
constructor(){
this.items = [];
}
//add elements to the queue - enqueue
addQueue(elements){
return this.items.push(elements) // push the elements into the empty array(items)
}
//Remove element from the queue - dequeue
RemoveQueue(){
if(this.items.length > 0){
return this.items.shift(); // if there is an elements in the array(items) remove the front item
}
}
//View the last element in the queue - peek
peek(){
return this.items[this.items.length -1]; //Return the last elements by subtracting -1
}
//Check if the queue is empty
isEmpty(){
return this.items.length == 0;
}
//The size of the queue - size
size(){
return this.items.length;
}
//Empt the entire queue
clear(){
this.items = [];
}
}
let newQueue = new Queue();
// Adding elements to the array
newQueue.addQueue('abel');
newQueue.addQueue('sam');
newQueue.addQueue('musa');
newQueue.addQueue('UD');
console.log(newQueue.items)
//The size of the array
console.log(`The size of the array is: ${newQueue.size()}`);
//Removing the last items
newQueue.RemoveQueue();
console.log(newQueue.items)
//view the last element in the array
console.log(`The last element in the array is: ${newQueue.peek()}`);
//Empty the array
console.log(`Is the array empty: ${newQueue.isEmpty()}`);