-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.html
More file actions
98 lines (84 loc) · 2.12 KB
/
Copy pathqueue.html
File metadata and controls
98 lines (84 loc) · 2.12 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Queue with javascript</h1>
<script>
let queue = [];
let queueSize = queue.length;
let maxSize = 6;
function enqueue(value) {
if (queueSize >= maxSize) {
console.log("Queue is full. Can't enqueue.");
return;
}
queue[queueSize] = value;
queueSize++;
}
function dequeue() {
if (queueSize === 0) {
console.log("Queue is empty. Can't dequeue.");
return;
}
let dequeueValue = queue[0];
for (let i = 0; i < queueSize - 1; i++) {
queue[i] = queue[i + 1];
}
queueSize--;
queue.length = queueSize;
return dequeueValue;
}
enqueue(50);
enqueue(100);
enqueue(150);
enqueue(200);
dequeue();
dequeue();
// console.log(queue);
// circular queue
class Queue {
constructor(size) {
this.max = size;
this.items = new Array(size);
this.currentSize = 0;
this.rear = -1;
this.front = -1;
}
enqueue(val) {
if (this.currentSize != this.max) {
if (this.rear == this.max - 1) {
this.rear = 0;
} else {
this.rear++;
}
this.items[this.rear] = val;
this.currentSize++;
if (this.front == -1) {
this.front = this.rear;
}
}
}
dequeue() {
if (this.currentSize != 0) {
this.items[this.front] = null;
if (this.front == this.max - 1) {
this.front = 0;
} else {
this.front++;
}
this.currentSize--;
} else {
this.front = -1;
this.rear = -1;
alert("queue is empty");
}
}
}
let queueCircle = new Queue(5);
</script>
</body>
</html>