KAITA

たぶんあってるとおもう。

※追記: この循環キューだとAOJ提出でTLE(制限時間超過)になったので書き換えたのをこっちに書いた

コード部のライセンスはAOJの利用規約に準拠します

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
class CircularQueue {

constructor(capacity) {
this._capacity = capacity;
this._items = new Array(capacity);
this._head = 0;
this._tail = 0;
}

_currentItemsCount() {
return this._items.filter(x => x !== undefined).length;
}

enqueue(item) {
if (this._currentItemsCount() >= this._capacity) {
throw new Error("Exceeded capacity");
}

this._items[this._tail] = item;

if (this._tail === this._capacity) {
this._tail = 0;
} else {
this._tail = this._tail + 1;
}
}

dequeue() {
if (this.isEmpty()) {
throw new Error("No items");
}

const head = this._items[this._head];
this._items[this._head] = undefined;

if (this._head === this._capacity) {
this._head = 0;
} else {
this._head = this._head + 1;
}

return head;
}

isEmpty() {
return this._currentItemsCount() === 0;
}

}

実行結果

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
const cq = new CircularQueue(5);
cq.enqueue("a");
cq.enqueue("b");
cq.enqueue("c");
cq.enqueue("d");
cq.enqueue("e");
console.log(cq._currentItemsCount());
console.log(cq.dequeue());
console.log(cq.dequeue());
console.log(cq.dequeue());
console.log(cq.dequeue());
console.log(cq.dequeue());
console.log(cq._currentItemsCount());

cq.enqueue("a");
cq.enqueue("b");
console.log(cq.dequeue());
cq.enqueue("c");
cq.enqueue("d");
cq.enqueue("e");
console.log(cq.dequeue());
console.log(cq.dequeue());
cq.enqueue("f");
console.log(cq.dequeue());
console.log(cq.dequeue());
console.log(cq.dequeue());

--------------
5
a
b
c
d
e
0
a
b
c
d
e
f

OWARI