export interface IQueue { getLength ( ) : number ; enqueue ( item ) : void ; dequeue ( ) : any ; peek ( ) : any ; isEmpty ( ) : boolean ; } export class Queue implements IQueue { private queue : any [ ] = [ ] ; private offset = 0 ; getLength ( ) { return this . queue . length - this . offset ; } enqueue ( item ) { this . queue . push ( item ) ; } dequeue ( ) { if ( this . queue . length == 0 ) return undefined ; var item = this . queue [ this . offset ] ; if ( ++ this . offset * 2 >= this . queue . length ) { this . queue = this . queue . slice ( this . offset ) ; this . offset = 0 ; } return item ; } peek ( ) { return ( this . queue . length > 0 ? this . queue [ this . offset ] : undefined ) ; } isEmpty ( ) { return this . queue . length == 0 ; } }