import { ErrorEnum, ERROR_EMPTY_ELEMENT, throwError } from '@/shared';
export interface IQueue<T> {
size: number;
top(): T;
enQueue(val: T): void;
deQueue(): T;
}
export class Queue<T> implements IQueue<T> {
private list: T[] = [];
get size() {
return this.list.length;
}
top(): T {
this.checkRange();
return this.list[0];
}
enQueue(val: T): void {
this.list.push(val);
}
deQueue(): T {
this.checkRange();
return this.list.shift()!;
}
private checkRange() {
if (this.size === 0) {
throwError(ERROR_EMPTY_ELEMENT, ErrorEnum.range);
}
}
}