This repository was archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.js
More file actions
67 lines (58 loc) · 1.23 KB
/
ArrayQueue.js
File metadata and controls
67 lines (58 loc) · 1.23 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
class ArrayQueue {
constructor(capacity) {
this._arr = Array(capacity || 100);
this._size = 0;
this._front = -1;
}
enqueue(data) {
if (this.isFull()) {
throw "Queue is full";
} else {
let position = (this._front + this._size) % this._arr.length;
this._arr[position] = data;
++this._size;
}
}
dequeue() {
if (this.isEmpty()) {
throw "Queue is Empty";
} else {
let data = this._arr[this._front];
this._front = (this._front + 1) % this._arr.length;
--this._size;
return data;
}
}
front() {
if (this.isEmpty()) {
throw new Error("Queue is empty");
} else {
return this._arr[this._front];
}
}
isEmpty() {
return this._size === 0;
}
isFull() {
return this._size === this._arr.length;
}
size() {
return this._size;
}
}
// main
// const q = new ArrayQueue(10);
// q.enqueue(1);
// q.enqueue(5);
// q.enqueue(10);
// q.enqueue(3);
// console.log(q.front());
// console.log(q.dequeue());
// console.log(q.dequeue());
// q.enqueue(50);
// console.log(q.dequeue());
// console.log(q.dequeue());
// console.log(q.size());
// console.log(q.front());
// console.log(q.dequeue());
// console.log(q.isEmpty());