import { ToString } from '@lib/model/to-string.interface' ; import { CircularLinkedListNode } from './circular-linked-list-node.class' ; export class CircularLinkedList < T > implements ToString { public start : CircularLinkedListNode < T > ; public end : CircularLinkedListNode < T > ; public constructor ( initialData ) { const mainLink = initialData [ 0 ] ; this . start = new CircularLinkedListNode ( mainLink ) ; this . end = this . start ; let skipped = false ; for ( const item of initialData ) { if ( skipped ) { this . add ( item ) ; } else { skipped = true ; } } } public add ( t ) { const link = new CircularLinkedListNode ( t , this . end , this . start ) ; this . end . next = link ; this . start . prev = link ; this . end = link ; } /**
     * Infinite iterator
     */ * [ Symbol . iterator ] ( ) { let current = this . start ; while ( current ) { yield current . value ; current = current . next ; } } public * singleIteration ( ) { let current = this . start ; while ( current !== this . end ) { yield current . value ; current = current . next ; } yield current . value ; } public toString ( ) { return [ ... this . singleIteration ( ) ] . join ( ',' ) ; } }