Skip to content

Instantly share code, notes, and snippets.

@nivrith
Created October 15, 2019 05:46
Show Gist options
  • Save nivrith/a05b32de3d2fdc97a8915b025fa8567f to your computer and use it in GitHub Desktop.
Save nivrith/a05b32de3d2fdc97a8915b025fa8567f to your computer and use it in GitHub Desktop.
Ts Queue
class Queue<T> {
constructor(private length: number, private data: T[] = []) {}
add(record) {
var length = this.data.unshift(record);
if (length > this.length) {
this.remove();
}
}
remove() {
this.data.pop();
}
size() {
return this.data.length;
}
isMember(item: T) {
if (this.data.indexOf(item) !== -1) {
return true;
} else {
return false;
}
}
// make queue iterable
public *[Symbol.iterator]() {
yield* [...this.data];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment