Created
October 15, 2019 05:46
-
-
Save nivrith/a05b32de3d2fdc97a8915b025fa8567f to your computer and use it in GitHub Desktop.
Ts Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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