-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33-QUEUE OPERATIONS USING LINKED LIST.js
More file actions
78 lines (61 loc) · 1.18 KB
/
Copy path33-QUEUE OPERATIONS USING LINKED LIST.js
File metadata and controls
78 lines (61 loc) · 1.18 KB
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
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
}
enqueue(value) {
const newNode = new Node(value);
if (!this.front) {
this.front = this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}
deqeue() {
if (!this.front) {
console.log("Queue is Empty");
return null;
}
const value = this.front.value;
this.front = this.front.next;
if (!this.front) {
this.rear = null;
}
return value;
}
peek() {
if (!this.front) {
console.log("Queue is Empty");
return null;
}
return this.front.value;
}
isEmpty() {
return this.front === null;
}
display() {
let current = this.front;
let result = "";
while (current) {
result += current.value + "->";
current = current.next;
}
console.log(result + "null");
}
}
const queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
// console.log(queue);
queue.display();
console.log(queue.deqeue());
queue.display();
console.log(queue.peek());