package lab04; public class TestADT { public static void main(String[] args) { QueueNode queue = new QueueNode(); queue.enqueue( 100 ); queue.enqueue( 200 ); queue.enqueue( 250 ); queue.enqueue( 350 ); queue.print( ); queue.dequeue( ); queue.dequeue( ); queue.print( ); LinkedListNode list = new LinkedListNode(); list.addRandomList(); System.out.println(); list.print( ); list.makeListLoopy(); checkListLoopy( list ); /* try { throw new Exception("Test exception"); } catch(MyException e) { System.out.println("catch bloc 1 " + e.toString()); } catch(Exception e) { System.out.println("catch bloc 2 " + e.toString()); } */ } static void checkListLoopy( LinkedListNode list ) { // Check if list is cyclic or not // Print the result; if the list is cyclic, print the cycle length // Use two pointers fast and slow that walk along the list at different // speeds LinkedListNode fast = list.next; LinkedListNode slow = list; // fast.nodeId == slow.nodeId int counter = 0; while( fast != null && fast.nodeId != slow.nodeId ) { fast = fast.next; if( counter % 2 == 1 ) slow = slow.next; counter++; } if ( fast == null ) { System.out.println(" The linked list has no cycles \n"); return; } fast = fast.next; counter = 0; while( fast.nodeId != slow.nodeId ) { fast = fast.next; counter++; } System.out.println(" The linked list has cycle of length = "+ counter +" \n"); } }