Difference between revisions of "CSC212 Lab 6 Solutions 2014"

From dftwiki3
Jump to: navigation, search
Line 100: Line 100:
 
</source>
 
</source>
 
<br />
 
<br />
<onlydft>
+
 
 
 
=Problem 3=
 
=Problem 3=
 
<br />
 
<br />
Line 159: Line 158:
 
=Problem 4=
 
=Problem 4=
 
<br />
 
<br />
 +
<onlydft>
 
<source lang="java">
 
<source lang="java">
 
/**
 
/**

Revision as of 18:44, 7 October 2014

--D. Thiebaut (talk) 13:04, 1 October 2014 (EDT)



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



...