44 lines
536 B
JavaScript
44 lines
536 B
JavaScript
export class Queue {
|
|
constructor() {
|
|
this.val = [];
|
|
this.ptr = 0;
|
|
}
|
|
|
|
size() {
|
|
return this.val.length - this.ptr;
|
|
}
|
|
|
|
get length() {
|
|
return this.size();
|
|
}
|
|
|
|
empty() {
|
|
return this.size() == 0;
|
|
}
|
|
|
|
front() {
|
|
return this.val[this.ptr];
|
|
}
|
|
|
|
_rebuild() {
|
|
if (this.ptr > 0) {
|
|
this.val = this.val.slice(this.ptr);
|
|
this.ptr = 0;
|
|
}
|
|
}
|
|
|
|
pop() {
|
|
this.ptr += 1;
|
|
if (this.ptr >= 16 && this.ptr >= this.val.length / 2) {
|
|
this._rebuild();
|
|
}
|
|
return this;
|
|
}
|
|
|
|
push(x) {
|
|
this.val.push(x);
|
|
return this;
|
|
}
|
|
};
|
|
|