Difference between revisions of "CSC212 Lab 6 Solutions 2014"
Line 8: | Line 8: | ||
<source lang="java"> | <source lang="java"> | ||
− | class | + | class IntSLLNode { |
public int info; | public int info; | ||
− | public | + | public IntSLLNode next; |
− | public | + | public IntSLLNode( int el ) { this( el, null ); } |
− | public | + | public IntSLLNode( int el, IntSLLNode n ) { info = el; next = n; } |
} | } | ||
public class Lab6_2 { | public class Lab6_2 { | ||
− | + | IntSLLNode head, tail; | |
int length; | int length; | ||
Line 30: | Line 30: | ||
public void addToHead( int el ) { | public void addToHead( int el ) { | ||
− | head = new | + | head = new IntSLLNode( el, head ); |
if ( tail==null ) | if ( tail==null ) | ||
tail = head; | tail = head; | ||
Line 40: | Line 40: | ||
addToHead( el ); | addToHead( el ); | ||
else { | else { | ||
− | tail.next = new | + | tail.next = new IntSLLNode( el ); |
tail = tail.next; | tail = tail.next; | ||
} | } | ||
Line 47: | Line 47: | ||
public void printAll() { | public void printAll() { | ||
− | for ( | + | for ( IntSLLNode it=head; it != null; it = it.next ) |
System.out.print( it.info + " " ); | System.out.print( it.info + " " ); | ||
System.out.println( ); | System.out.println( ); |