-
Notifications
You must be signed in to change notification settings - Fork 0
/
74. Implement_a_queue.js
81 lines (73 loc) · 1.54 KB
/
74. Implement_a_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
69
70
71
72
73
74
75
76
77
78
79
80
81
//*----- implement a queue
//? You can implement a queue data structue in javascirpt using an array.
//* method 1 : using array : push and shift methods
//* note :- problem with this method : Remember that both the push and pop methods have a time complexity of O(1)? The shift method has a time complexity of O(n).
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpty()) {
return "Queue is empty";
} else {
return this.items.shift();
}
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
} else {
return this.items[0];
}
}
print() {
console.log(this.items);
}
}
//* method 2 - Queue optimised implementation - using object
class Queue {
constructor() {
this.items = {};
this.rear = 0;
this.front = 0;
}
enqueue(element) {
this.items[this.rear] = element;
this.rear++;
}
dequeue() {
const item = this.items[this.front];
delete this.items[this.front];
this.front++;
return item;
}
size() {
return this.rear - this.front;
}
isEmpty() {
return this.size() === 0;
}
peek() {
return this.items[this.front];
}
print() {
console.log(this.items);
}
}
let queue = new Queue();
console.log(queue.isEmpty());
queue.enqueue(14);
queue.enqueue(24);
queue.enqueue(44);
console.log(queue.dequeue());
console.log(queue.size());
queue.print();