Difference between revisions of "CSC212 Lab 6 Solutions 2014"
(9 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
---- | ---- | ||
<br /> | <br /> | ||
− | < | + | =Problem 1= |
+ | <br /> | ||
+ | <source lang="java"> | ||
+ | |||
+ | class IntSLLNode { | ||
+ | public int info; | ||
+ | public IntSLLNode next; | ||
+ | public IntSLLNode( int el ) { this( el, null ); } | ||
+ | public IntSLLNode( int el, IntSLLNode n ) { info = el; next = n; } | ||
+ | } | ||
+ | |||
+ | public class Lab6_1 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | IntSLLNode head, tail; | ||
+ | head = null; | ||
+ | tail = null; | ||
+ | |||
+ | head = new IntSLLNode( 5, head ); | ||
+ | tail = head; | ||
+ | |||
+ | head = new IntSLLNode( 10, head ); | ||
+ | |||
+ | tail.next = new IntSLLNode( 3, null ); | ||
+ | tail = tail.next; | ||
+ | |||
+ | for ( IntSLLNode it = head; it != null; it = it.next ) { | ||
+ | System.out.println( it.info ); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
=Problem 2= | =Problem 2= | ||
<br /> | <br /> | ||
− | + | <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 | + | public class MyLinkedList { |
− | + | IntSLLNode head, tail; | |
int length; | int length; | ||
− | public | + | public MyLinkedList() { |
head = tail = null; | head = tail = null; | ||
length = 0; | length = 0; | ||
Line 30: | Line 61: | ||
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 71: | ||
addToHead( el ); | addToHead( el ); | ||
else { | else { | ||
− | tail.next = new | + | tail.next = new IntSLLNode( el ); |
tail = tail.next; | tail = tail.next; | ||
+ | length++; | ||
} | } | ||
− | + | ||
} | } | ||
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( ); | ||
Line 66: | Line 98: | ||
System.out.println( "+===========================================\n| List:"); | System.out.println( "+===========================================\n| List:"); | ||
System.out.println( isEmpty()? "| is empty": "| is not empty" ); | System.out.println( isEmpty()? "| is empty": "| is not empty" ); | ||
− | System.out.println( "| contains " + length + " | + | System.out.println( "| contains " + length + " element" + ((length!=1)? "s":"" ) ); |
System.out.print( "| elements: " ); | System.out.print( "| elements: " ); | ||
printAll(); | printAll(); | ||
Line 73: | Line 105: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | + | MyLinkedList L = new MyLinkedList(); | |
L.printStatus( "Brand new list" ); | L.printStatus( "Brand new list" ); | ||
Line 82: | Line 114: | ||
L.printStatus( "After adding 10, 20, ... to tail..."); | L.printStatus( "After adding 10, 20, ... to tail..."); | ||
− | L = new | + | L = new MyLinkedList(); |
L.printStatus( "Brand new list" ); | L.printStatus( "Brand new list" ); | ||
Line 100: | Line 132: | ||
</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 /> | ||
+ | =Problem 4= | ||
+ | <br /> | ||
+ | <onlydft> | ||
+ | <source lang="java"> | ||
+ | /** | ||
+ | * A word scanner. | ||
+ | * @author D. Thiebaut | ||
+ | */ | ||
+ | import java.util.Scanner; | ||
+ | import java.util.Stack; | ||
+ | |||
+ | public class Lab6_4 { | ||
+ | |||
+ | /** | ||
+ | * main entry point | ||
+ | * @param args | ||
+ | */ | ||
+ | public static void main( String[] args ) { | ||
+ | Scanner inputScanner = new Scanner( System.in ); | ||
+ | Stack stack = new Stack(); | ||
+ | |||
+ | while ( inputScanner.hasNextLine() ) { | ||
+ | String line = inputScanner.nextLine(); | ||
+ | System.out.println( line ); | ||
+ | Scanner wordScanner = new Scanner( line ); | ||
+ | |||
+ | while ( wordScanner.hasNext() ) | ||
+ | stack.push( (String) wordScanner.next() ); | ||
+ | |||
+ | |||
+ | while ( !stack.isEmpty() ) | ||
+ | System.out.print( (String) stack.pop() + " "); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | <br /> | ||
</onlydft> | </onlydft> | ||
<br /><br /><br /><br /><br /> | <br /><br /><br /><br /><br /> |
Latest revision as of 18:45, 7 October 2014
--D. Thiebaut (talk) 13:04, 1 October 2014 (EDT)
Contents
Problem 1
class IntSLLNode {
public int info;
public IntSLLNode next;
public IntSLLNode( int el ) { this( el, null ); }
public IntSLLNode( int el, IntSLLNode n ) { info = el; next = n; }
}
public class Lab6_1 {
public static void main(String[] args) {
IntSLLNode head, tail;
head = null;
tail = null;
head = new IntSLLNode( 5, head );
tail = head;
head = new IntSLLNode( 10, head );
tail.next = new IntSLLNode( 3, null );
tail = tail.next;
for ( IntSLLNode it = head; it != null; it = it.next ) {
System.out.println( it.info );
}
}
}
Problem 2
class IntSLLNode {
public int info;
public IntSLLNode next;
public IntSLLNode( int el ) { this( el, null ); }
public IntSLLNode( int el, IntSLLNode n ) { info = el; next = n; }
}
public class MyLinkedList {
IntSLLNode head, tail;
int length;
public MyLinkedList() {
head = tail = null;
length = 0;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead( int el ) {
head = new IntSLLNode( el, head );
if ( tail==null )
tail = head;
length++;
}
public void addToTail( int el ) {
if ( tail == null )
addToHead( el );
else {
tail.next = new IntSLLNode( el );
tail = tail.next;
length++;
}
}
public void printAll() {
for ( IntSLLNode it=head; it != null; it = it.next )
System.out.print( it.info + " " );
System.out.println( );
}
public int deleteFromHead() {
int el = head.info;
if ( tail==head )
head = tail = null;
else
head = head.next;
length--;
return el;
}
public void printStatus( String caption ) {
System.out.println( "+===========================================\n|" + caption );
System.out.println( "+===========================================\n| List:");
System.out.println( isEmpty()? "| is empty": "| is not empty" );
System.out.println( "| contains " + length + " element" + ((length!=1)? "s":"" ) );
System.out.print( "| elements: " );
printAll();
System.out.println( "+===========================================" );
}
public static void main(String[] args) {
MyLinkedList L = new MyLinkedList();
L.printStatus( "Brand new list" );
for ( int i=10; i<50; i+= 10 )
L.addToTail( i );
L.printStatus( "After adding 10, 20, ... to tail...");
L = new MyLinkedList();
L.printStatus( "Brand new list" );
for ( int i=5; i<50; i+= 10 )
L.addToHead( i );
L.printStatus( "After adding 5, 15, ... to head...");
while ( ! L.isEmpty() ) {
int el = L.deleteFromHead();
L.printStatus( "After removing " + el );
}
}
}
Problem 3
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 );
}
}
}
Problem 4