-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (50 loc) · 1.83 KB
/
script.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
let queue = [];
let front = 0;
let rear = -1;
function enqueue() {
const nilaiInput = document.getElementById('nilaiInput').value;
if (nilaiInput !== '') {
queue.push(parseInt(nilaiInput));
rear++;
document.getElementById('message').innerText = `Nilai ${nilaiInput} berhasil ditambahkan ke antrian.`;
document.getElementById('nilaiInput').value = '';
} else {
alert('Masukkan nilai terlebih dahulu!');
}
}
function dequeue() {
if (front > rear) {
document.getElementById('message').innerText = 'Antrian kosong!';
} else {
const removed = queue.shift();
rear--;
document.getElementById('message').innerText = `Nilai ${removed} dihapus dari antrian.`;
}
}
function showQueue() {
const indexRow = document.getElementById('indexRow');
const valueRow = document.getElementById('valueRow');
// Bersihkan baris sebelumnya
indexRow.innerHTML = '<td>Index</td>';
valueRow.innerHTML = '<td>Isi</td>';
// Menambahkan kolom secara dinamis sesuai data antrian
for (let i = 0; i < queue.length; i++) {
const indexCell = document.createElement('td');
indexCell.innerText = front + i + 1; // Menampilkan nomor indeks
indexRow.appendChild(indexCell);
const valueCell = document.createElement('td');
valueCell.innerText = queue[i]; // Menampilkan nilai pada antrian
valueRow.appendChild(valueCell);
}
// Menampilkan posisi front dan rear
document.getElementById('indexFront').innerText = `Index Front = ${front + 1}`;
document.getElementById('indexRear').innerText = `Index Rear = ${rear + 1}`;
// Tampilkan popup
document.getElementById('popup').style.display = 'block';
}
function showCount() {
document.getElementById('message').innerText = `Jumlah antrian saat ini: ${queue.length}`;
}
function closePopup() {
document.getElementById('popup').style.display = 'none';
}