-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathqueue_with_input_elements.html
87 lines (78 loc) · 2.03 KB
/
queue_with_input_elements.html
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
<html>
<head>
<title>Queue in JavaScript</title>
<script>
let queue = [];
let currentSize = queue.length;
let maxSize = 5;
// function enqueue(newVal) {
// if (currentSize >= maxSize) {
// alert("Queue is already full");
// } else {
// queue[currentSize] = newVal;
// currentSize++;
// }
// }
function enqueueWithBtn() {
let newVal = document.getElementById("qEl").value;
if (currentSize >= maxSize) {
alert("Queue is already full");
} else {
queue[currentSize] = newVal;
currentSize++;
}
}
function display() {
console.warn(queue);
}
function dequeue() {
if (!isEmpty()) {
for (let i = 0; i < queue.length; i++) {
queue[i] = queue[i + 1];
}
currentSize--;
queue.length = currentSize;
} else {
alert("queue is already empty");
}
}
function front() {
if (!isEmpty()) {
console.warn(queue[0]);
} else {
alert("queue is already empty");
}
}
function rear() {
if (!isEmpty()) {
console.warn(queue[currentSize - 1]);
} else {
alert("queue is already empty");
}
}
function isEmpty() {
if (currentSize <= 0) {
return true;
} else {
return false;
}
}
// enqueue(10);
// enqueue(20);
// enqueue(30);
// front();
// rear();
// display();
</script>
</head>
<body>
<h1>Queue with input elements in JavaScript</h1>
<input placeholder="enter element" id="qEl" />
<button onclick="enqueueWithBtn()">Add Element</button>
<br/><br/>
<button onclick="dequeue()">Remove Element</button>
<button onclick="display()">Display</button>
<button onclick="front()">Get Front Element</button>
<button onclick="rear()">Get Rear Element</button>
</body>
</html>