Difference between revisions of "CSC212 Lab 6 Solutions 2014"
Line 100: | Line 100: | ||
</source> | </source> | ||
+ | <br /> | ||
+ | =Problem 3= | ||
+ | <br /> | ||
+ | <source lang="java"> | ||
+ | import java.util.Iterator; | ||
+ | import java.util.LinkedList; | ||
+ | |||
+ | public class Lab6_3 { | ||
+ | |||
+ | |||
+ | public static void printStatus( LinkedList L, String caption ) { | ||
+ | System.out.print( caption + "\nL = " ); | ||
+ | Iterator it = L.iterator(); | ||
+ | while ( it.hasNext() ) { | ||
+ | int el = (int) it.next(); | ||
+ | System.out.print( el + " " ); | ||
+ | } | ||
+ | System.out.println( ); | ||
+ | |||
+ | } | ||
+ | public static void main(String[] args) { | ||
+ | LinkedList L = new LinkedList(); | ||
+ | |||
+ | for ( int i=10; i<=50; i+= 10 ) | ||
+ | L.addFirst( i ); | ||
+ | |||
+ | printStatus( L, "After adding 10, 20, 30, 40, 50..." ); | ||
+ | |||
+ | L = new LinkedList(); | ||
+ | |||
+ | printStatus( L, "Brand new list" ); | ||
+ | |||
+ | for ( int i=10; i<=50; i+= 10 ) | ||
+ | L.addLast( i ); | ||
+ | |||
+ | printStatus( L, "After adding 10, 20, ...50 to tail..."); | ||
+ | |||
+ | L = new LinkedList(); | ||
+ | printStatus( L, "Brand new list" ); | ||
+ | |||
+ | for ( int i=5; i<50; i+= 10 ) | ||
+ | L.addFirst( i ); | ||
+ | |||
+ | printStatus( L, "After adding 5, 15, ... to head..."); | ||
+ | |||
+ | while ( ! L.isEmpty() ) { | ||
+ | int el = (int) L.removeFirst(); | ||
+ | printStatus( L, "After removing " + el ); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
</onlydft> | </onlydft> | ||
<br /><br /><br /><br /><br /> | <br /><br /><br /><br /><br /> |